PHP uses recursive functions to create multilevel catalogs

Source: Internet
Author: User
Tags explode
This article mainly introduces PHP using recursive function to create a multilevel directory, interested in a friend's reference, I hope to be helpful to everyone.

Specific as follows:

function Directory ($dir) {if (Is_dir ($dir) | | @mkdir ($DIR, 0777)) {//See if the directory already exists or try to create, plus an @ suppress symbol because the first creation fails, it will report a "parent directory does not exist"        The warning. Echo $dir. " Create success <br> ";  The output creates a successful directory}else{$DIRARR =explode ('/', $dir);//When the subdirectory is not created successfully, try to create the parent directory, using the explode () function to cut the '/' delimiter into an array array_pop ($ Dirarr); Bounces the last item in the array (that is, the subdirectory), $newDir =implode ('/', $DIRARR); Regroup into a folder string Directory ($newDir); An attempt was made to create the parent directory if (@mkdir ($dir, 0777)) {echo $dir.        Create success <br> "; }//try to create subdirectories again, successfully output directory name}}directory ("a/b/c/d/e/f");

Output results

But it can be seen that the writing is too troublesome, in the manual to look at the file function, see a dirname () function , the prototype is as follows:

String DirName (String $path)

Gives a string containing a full path to a file, which returns the directory name after removing the file name.

In Windows, slashes (/) and backslashes (\) can be used as directory separators. In other environments it is a slash (/).

You can optimize it a little bit:

function Directory ($dir) {if (Is_dir ($dir) | | @mkdir ($DIR, 0777)) {echo $dir.    Create success <br> ";  }else{Directory (dirname ($dir)); if (@mkdir ($dir, 0777)) {echo $dir.        Create success <br> "; }    }}

The same effect.
After I searched the internet for answers, I found an exceptionally incisive :

function Directory ($dir) {     return  Is_dir ($dir) or Directory (DirName ($dir))  and mkdir ($dir, 0777);}

Now let's explain the whole function:

Let's start with the order of precedence of logical operators in PHP :&& > | | > and > or, symbol > Letter type, and type >or, so the function body can be considered as:

Is_dir ($dir) or (Directory (dirname ($dir)) and mkdir ($dir, 0777));

First determine whether the target directory exists, if present, according to or short-circuit characteristics, the whole behind is short-circuiting, skip execution; If the target directory does not exist, the following function body is executed:

Directory (DirName ($dir)) and mkdir ($dir, 0777)

I thought about recursion first : recursion is performed first to confirm that its parent directory (DirName ($DIR)) has been created so that the mkdir () function does not create a subdirectory when the parent directory is not found to issue a warning.

After entering the recursive depths, after confirming that the deepest root directory exists, create the directory down from the root directory.

Finally, it is recommended to find work of the pro, go online to find some big company face test to do, after all, they test more comprehensive, in learning knowledge, also brush the title, also must do a bit, because it is easy to above his business, the beginning of the function, I optimized several times to normal use.

This is a PHP recursive creation of multi-level directory interview topics, after the small series will find some interesting questions to share with you.

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related Article

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.