PHP function mkdir Implementation recursively create hierarchical directory _php instance

Source: Internet
Author: User
Tags explode mkdir parent directory

The following is my data from the Internet, will give you a detailed introduction of PHP function mkdir specific use, I hope that there is a need for friends to help.

In the development of the project, it is unavoidable to create folders on the server, such as the directory when uploading pictures, the directory of template parsing. This does not work with the current project, so it summarizes several ways to create a hierarchical directory.

PHP default mkdir can only create one level of directory at a time, and to create levels of each level of directories, the general is created from the father, and then layer down to create, but this is a bit too cumbersome to create.

What do we do when we write a program? Soon to be able to automate the implementation of the functions we need, the approach here is to be able to help us automate the creation of the completion level directory through the program.

There are two ways of thinking:

One, from the top down (parent → Child)

1. First determine whether the parent directory exists, does not exist to create;
2. To determine if the level two subdirectory exists, can not exist to create,
3. In the second step, recursively calls the function itself as a subdirectory as a parameter.

Two, from the bottom up (child → parent)

1. First of all, determine whether the bottom directory exists;
2. To determine whether the upper-level directory of the underlying directory exists, does not exist in the upper directory as a parameter recursive.

Here are a few ways:

1: The recursive creation of the directory, this method is my current feeling better way.

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: Recursively create a cascading directory, if the first method is not very understanding, you can combine the following method to understand

function MkDirs1 ($path) {
  if (Is_dir ($path)) {//is already a directory without creating a return
    true;
  }
  if (Is_dir (DirName ($path)) {//parent directory already exists, create return
    mkdir ($path) directly;
  }
  MkDirs1 (DirName ($path));//Create
  a return mkdir ($path) from a subdirectory;//Because there is a parent directory, you can create a path
}
//mkdirs1 (' 1/2/3/');

3: Iterate to create a cascading directory

function MakeDir ($path) {
  $arr =array ();
  while (!is_dir ($path)) {
    Array_push ($arr, $path);//Push all levels of the parent directory in the path into the array, directly with the parent directory existence (that is, the line above Is_dir to determine the directory, The condition is a false exit while loop)
    $path =dirname ($path);//The Parent directory
  }
  if (Empty ($arr)) {//arr is null to prove that the while loop above does not execute, that the directory already exists
    echo $path, ' already exist ';
    return true;
  }
  while (count ($arr)) {
    $parentdir =array_pop ($arr);//Eject 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 files created need to be created in 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 entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.