PHP创建嵌套目录

sun*_*jie 26 php directory mkdir

我需要一个函数的帮助来为以下情况创建一个2级目录:

  1. 所需的子目录存在于父目录中,不执行任何操作.
  2. 父目录存在,子目录不存在.仅创建子目录.
  3. 既不存在父目录,也不存在子目录,首先创建父目录,然后创建子目录.
  4. 如果未成功创建任何目录,则返回FALSE.

谢谢您的帮助.

Kin*_*nch 59

使用第三个参数mkdir():

recursive允许创建路径名中指定的嵌套目录.默认为FALSE.

$path = '/path/to/folder/with/subdirectory';
mkdir($path, 0777, true);
Run Code Online (Sandbox Code Playgroud)

  • 使用if(!is_dir($ path)){mkdir($ path,0777,true); } (3认同)

小智 5

recursive 允许创建在路径名中指定的嵌套目录。但对我不起作用!!因为这就是我想出的!!它工作得非常完美!!

$upPath = "../uploads/RS/2014/BOI/002";   // full path 
$tags = explode('/' ,$upPath);            // explode the full path
$mkDir = "";

    foreach($tags as $folder) {          
        $mkDir = $mkDir . $folder ."/";   // make one directory join one other for the nest directory to make
        echo '"'.$mkDir.'"<br/>';         // this will show the directory created each time
        if(!is_dir($mkDir)) {             // check if directory exist or not
          mkdir($mkDir, 0777);            // if not exist then make the directory
        }
    }
Run Code Online (Sandbox Code Playgroud)