Home  >  Article  >  Backend Development  >  php mkdir creates multi-level directory example code_PHP tutorial

php mkdir creates multi-level directory example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:49:19930browse

php mkdir can only create directories one level at a time. If there are multiple levels, we need to traverse recursively to create directories. Let me introduce to you some tips on how to use this function.

First introduce the mkdir() function

mkdir($path,0777,true);

The first parameter: required, represents the path of the multi-level directory to be created;

The second parameter: Set the permissions of the directory, the default is 0777, which means the maximum possible access rights;

The third parameter: true means allowing the creation of multi-level directories.

mkdir($dir,$mode); but it can only create one directory at a time, which means it cannot create multi-level directories at once, as follows

mkdir('aa'); //Only one aa directory can be created

mkdir('aa/bb/cc');//If there is an aa/bb directory, you can successfully create the cc directory, otherwise an error will be reported. If you want to create multiple directories, let’s look at the code below

Example code (supports creation of Chinese directories):

The code is as follows
 代码如下 复制代码


header("Content-type:text/html;charset=utf-8");

//要创建的多级目录

$path="dai/php/php学习";

//判断目录存在否,存在给出提示,不存在则创建目录

if (is_dir($path)){

echo "对不起!目录 " . $path . " 已经存在!";

}else{

//第三个参数是“true”表示能创建多级目录,iconv防止中文目录乱码

$res=mkdir(iconv("UTF-8", "GBK", $path),0777,true);

if ($res){

echo "目录 $path 创建成功";

}else{

echo "目录 $path 创建失败";

}

}


?>

Copy code

代码如下 复制代码

/*
*mkdir($dir,$mode)
*PHP 递归创建目录
*/
function mkdirs($dir, $mode = 0777)
{
if (is_dir($dir) || @mkdir($dir, $mode)){
return true;
}
if (!mkdirs(dirname($dir), $mode)){
return false;
}
return @mkdir($dir, $mode);
}

function mkdirs($dir, $mode = 0777)
{
$dirArray = explode("/",$dir);
$dirArray = array_filter($dirArray);

$created = "";
foreach($dirArray as $key => $value){
        if(!empty($created)){
            $created .= "/".$value;
            if(!is_dir($created)){
                mkdir($created,$mode);
            }
        }else{
            if(!is_dir($value)){
                mkdir($value,$mode);
            }
            $created .= $value;
        }
    }
}
?>
//代码应用实例
$path="abc/ff/ss/";
mkdirs($path,$mode = 0777);


header("Content-type:text/html;charset=utf-8"); //Multi-level directories to be created $path="dai/php/phplearning"; // Determine whether the directory exists and give a prompt if it exists. If it does not exist, create the directory if (is_dir($path)){ echo "Sorry! Directory " . $path . " already exists!"; }else{ //The third parameter is "true", which means that multi-level directories can be created. iconv prevents Chinese directories from being garbled $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true); if ($res){ echo "Directory $path created successfully"; }else{ echo "Failed to create directory $path"; } }
?> Look at another example of recursively creating a directory The editor has compiled two methods for creating directories recursively for your reference. Thank you!
The code is as follows Copy code
/*
*mkdir($dir,$mode)
*PHP creates directories recursively
*/
function mkdirs($dir, $mode = 0777)
{
If (is_dir($dir) || @mkdir($dir, $mode)){
         return true;
}
If (!mkdirs(dirname($dir), $mode)){
         return false;
}
Return @mkdir($dir, $mode);
}<🎜> <🎜>function mkdirs($dir, $mode = 0777)
{
$dirArray = explode("/",$dir);
$dirArray = array_filter($dirArray);

$created = "";
foreach($dirArray as $key => $value){
          if(!empty($created)){
                $created .= "/".$value;
                 if(!is_dir($created)){
                     mkdir($created,$mode);
             }
          }else{
                if(!is_dir($value)){
                      mkdir($value,$mode);
             }
                 $created .= $value;
         }
}
}
?>
//Code application example
$path="abc/ff/ss/";
mkdirs($path,$mode = 0777); What is introduced above is the content of PHP’s recursive creation of directories and multi-level directories

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632720.htmlTechArticlephp mkdir can only create directories one level at a time. If there are multiple levels, we need to recursively traverse to create the directory. , let me introduce to you some tips on using this function. Let me introduce first...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn