PHP function mkdir implement recursive creation of hierarchical directory

Source: Internet
Author: User
Tags explode
Here is the information I collated from the Internet, will give you a detailed introduction of PHP function mkdir specific usage, I hope to have this need to help friends.

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.

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)) {//is already a directory, you do not have to create    return true;  }  if (Is_dir (DirName ($path))) {//parent directory already exists, directly create    return mkdir ($path);  }  MkDirs1 (DirName ($path));/  /Create a return mkdir ($path) from the subdirectory and/////Because there is a parent directory, you can create a path}//mkdirs1 (' 1/2/3/');

3: Iterate to create cascading catalogs

function MakeDir ($path) {  $arr =array ();  while (!is_dir ($path)) {    Array_push ($arr, $path),//press the parent directory in the path into the array, directly with the parent directory exists (that is, the above row is_dir to determine the directory, Condition is false exit while loop)    $path =dirname ($path);//parent Directory  }  if (Empty ($arr)) {//arr is empty proof that the while loop above is not executed, that the directory already exists    echo $path, ' already exists ';    return true;  }  while (count ($arr)) {    $parentdir =array_pop ($arr);//pops up the last array cell    mkdir ($PARENTDIR);//create from parent directory  }} 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.

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);}}  }

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

  • 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.