PHP recursive creation of multilevel catalogs (an interview problem solving process)
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 the directory already exists or try to create it, [email protected] will report a "parent directory does not exist" warning. Echo $dir." Created successfully
";//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." Created successfully
"; } //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." Created successfully
"; }Else{Directory (dirname($dir)); if(@mkdir($dir, 0777)) {Echo $dir." Created successfully
"; }}}
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.