PHP recursive creation of multilevel catalogs (an interview problem solving process)

Source: Internet
Author: User

See an interview today to write a function that can create a multilevel directory:

My first feeling is to use recursion to create a specific idea as follows:

functionDirectory ($dir){if(Is_dir($dir) || @mkdir($dir, 0777)) {//To see if a directory already exists or is trying to create, plus an @ suppress symbol because the first creation fails, a warning "parent directory does not exist" is reported.           Echo $dir." Create success <br> ";//output to create a successful directory}Else{$DIRARR=Explode(‘/‘,$dir);//When a subdirectory is not created successfully, an attempt is made to create a 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 stringDirectory ($newDir);//trying to create a parent directory          if(@mkdir($dir, 0777)) {Echo $dir." Create Success <br> "; } //attempt to create subdirectory 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:

functionDirectory ($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  $dir ) {         return   is_dir$dir )  or  Directory (dirname $dir ))  and   mkdir$dir , 0777);}

Now let's explain the whole function:

Let's start by introducing the precedence order of logical operators in PHP:&& > | | > and > or, which are symbolic > alphabetic, 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.

I will also find some interesting questions to share with you later.

If you think this blog is helpful to you, you can recommend or follow me, if you have any questions, you can leave a comment under the discussion, thank you.

PHP recursive creation of multilevel catalogs (an interview problem solving process)

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.