search
Homephp教程PHP源码php mkdir 创建多级目录实例代码

php mkdir创建目录只能一级级创建目录了,如果多级我们需要递归遍历来创建目录哦,下面我给大家介绍此函数的一些使用技巧吧。

<script>ec(2);</script>

先介绍一下 mkdir() 这个函数

mkdir($path,0777,true);

第一个参数:必须,代表要创建的多级目录的路径;

第二个参数:设定目录的权限,默认是 0777,意味着最大可能的访问权;

第三个参数:true表示允许创建多级目录。

mkdir($dir,$mode);但是它每次只能创建一个目录,也就是说它不能一次创建多级目录,如下

mkdir('aa'); //就只能创建一个aa目录了

mkdir('aa/bb/cc');//如果有aa/bb目录就可以成功创建cc目录否则会报错哦,如果要创建多目录我们看下面代码

举例代码(支持创建中文目录):

 代码如下 复制代码

 


 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 创建失败";

  }

 }

 


?>

再看一个递归创建目录的实例

小编整理了两个可以递归创建目录的方法供大家参考学习,谢谢啦!

 代码如下 复制代码

/*
*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);

上边介绍的就是php递归创建目录和多级目录的内容

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use