Create a hierarchical directory with PHP functions mkdir recursively

Source: Internet
Author: User
Tags explode

Project development is unavoidable to create folders on the server, such as when uploading images of the directory, template parsing directory, and so on. This is not the current project to use this, so summed up a few loops to create a hierarchical directory method.

PHP default mkdir can only create one level of directory at a time, and to create each level of the directory, it is usually first created from the parent, and then layer by piece, but this is created by hand, a bit too much trouble.

What do we do with the program? In the near future to automate the implementation of the functionality we need, here's a way to help us automate the creation of a complete hierarchy of directories.

Ruesin.com

There are two ways of thinking:
First, down from the top (parent → Child)
1. First to determine whether the parent directory exists, does not exist to create;
2. Determine if the level two subdirectory exists, cannot exist, or create the
3. In the second step, the function itself is called recursively as a sub-directory.

Second, from bottom to top (child → parent)
1. First determine if the lowest level directory exists;
2. Determine if the upper level directory of the underlying directory exists and does not exist, recursively with the upper directory as a parameter.

Here are a few methods:

1: Recursive creation of the directory, this method is my current feeling better method.

function mkDirs($dir){    if(!is_dir($dir)){        if(!mkDirs(dirname($dir))){            return false;        }        if(!mkdir($dir,0777)){            return false;        }    }    return true;}mkDirs(‘1/2/3/‘);

2: Recursive creation of cascading directories, if the first method is not very understanding, can be combined with the following method to understand

function mkDirs1($path){    if(is_dir($path)){//已经是目录了就不用创建        return true;    }    if(is_dir(dirname($path))){//父目录已经存在,直接创建        return mkdir($path);    }    mkDirs1(dirname($path));//从子目录往上创建    return mkdir($path);//因为有父目录,所以可以创建路径}//mkDirs1(‘1/2/3/‘);

3: Iterate to create cascading catalogs

function makedir($path){    $arr=array();    while(!is_dir($path)){        array_push($arr,$path);//把路径中的各级父目录压入到数组中去,直接有父目录存在为止(即上面一行is_dir判断出来有目录,条件为假退出while循环)        $path=dirname($path);//父目录    }    if(empty($arr)){//arr为空证明上面的while循环没有执行,即目录已经存在        echo $path,‘已经存在‘;        return true;    }    while(count($arr)){        $parentdir=array_pop($arr);//弹出最后一个数组单元        mkdir($parentdir);//从父目录往下创建    }}makedir(‘1/2/3‘);

PS: Sometimes the file of the program script is not necessarily in the root directory of the site, and the created file needs to be created at the root directory, then we need to use the site root directory path: $_server[' document_root ']; so I don't recommend using the following three methods.
The following three types are created by means of the '/' Split path.

This article from Ruesin Blog

function mk1($dir){    $arr=explode("/",$dir);    $path=‘‘;    for($i=0;$i< count($arr);$i++){        $path.=$arr[$i].‘/‘;        if(!is_dir($path)){            mkdir($path,0777);        }    }}function mk2($dir){    static $i=0;    $path=‘‘;    $arr=explode("/",$dir);    if($i < count($arr)){        $path.=$arr[$i];        mkdir($path,0777,true);        $i++;        mk2($path."/");    }}function mkdirs0($path){    $dirs = explode(‘/‘,$path);    for ($c=0;$c < count($dirs); $c++) {        $thispath="";        for ($cc=0; $cc <= $c; $cc++) {            $thispath.=$dirs[$cc].‘/‘;        }        if (!is_dir($thispath)) {            mkdir($thispath,0777);        }    }}

Please leave the minimum copyright to the Blogger, thank you!

Reprint please specify: Use PHP function mkdir recursive to create a hierarchical directory

Create a hierarchical directory with PHP functions mkdir recursively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.