Before I start, let me explain that many friends may think like me that mkdir can create a folder as long as you give a path. Others are not like that. A single MKDIR can only create one-level directories, but not multi-level directories. So how to use mkdir to create it? First, let me copy a description of mkdir from the manual, as follows:
Explanation
bool mkdir ( string pathname [, int mode [, bool recursive [, resource context]]] )
Try to create a new directory specified by pathname.
Note that you may want to specify the mode as an octal number, which means the number should start with zero. The mode is also modified by the current umask, which can be changed with umask().
Note: mode is ignored under Windows. Optional since PHP 4.2.0.
The default mode is 0777, which means the greatest possible access. For more information about mode please read the chmod() page. Example 1. mkdir() example
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>mkdir("/path/<br />to/my/dir", 0700); </span></li><li class="alt"><span class="tag">?></span><span> </span></span></li></ol>
Returns TRUE if successful and FALSE if failed.
Note: Since PHP 5.0.0 rmdir() can also be used with certain URL wrapping protocols. See Appendix N for a list of which URL encapsulation protocols rmdir() supports.
Note: Support for context was added in PHP 5.0.0. For a description of context, see Reference CLX, Stream Functions.
Note: The recursive parameter was added in PHP 5.0.0.
Note: When safe mode is activated, PHP will check whether the directory being manipulated has the same UID (owner) as the script being executed.
The above is the description information in the PHP5 manual, that is, you can create a folder like this: mkdir('./test',0777). But how to create multi-level directories recursively?
The ways to create folders in PHP are:
1 The directory creation function mkdir under PHP5 adds a new parameter recursive. By setting recursive to true, the purpose of creating a directory recursively can be achieved, but for PHP4 That won’t work.
2 Write a recursive method to create multi-level directories.
Here, I will give a little explanation on the second method. There are two methods, as follows:
The first PHP creates a folder (use mkdirs to generate multi-level parent :
mkdir()can only create folders in existing directories (that is, the parent must exist).
mkdirs()<ol class="dp-xml"> <li class="alt"><span><span>function mkdirs($dir, $</span><span class="attribute">mode</span><span> = </span><span class="attribute-value">0777</span><span>) </span></span></li> <li><span>{ </span></li> <li class="alt"><span>if (is_dir($dir) || <br>@mkdir($dir, $mode)) <br>return TRUE; </span></li> <li><span>if (!mkdirs(dirname<br>($dir), $mode)) return FALSE; </span></li> <li class="alt"><span>return @mkdir($dir, $mode); </span></li> <li><span>} </span></li> </ol>
2 The general process is: (1) First use is_dir to determine whether it is already a folder. If so Returns TRUE. If it doesn't exist (or it's not a folder), try to create it. Of course, the parent may not exist, then mkdir will not be created successfully, but it will not report an error, so use @ to suppress non-fatal errors.
(2) If none of the passed parameters meet the conditions, then enter the second if statement. First, obtain the directory part of the path. Of course, it may be a multi-level parent that does not exist, so mkdirs is used () to create the parent first. If successful (returns FALSE if unsuccessful), then use mkdir to create the final directory.
Okay, the above is the first option.
The second type of PHP creates a folder(Note: This solution is quite streamlined, that is a pretty good solution, it is recommended to use it)
Instructions:
1 General process: After getting the path, first determine whether it is a valid file directory , if so, return and end the program. If not, (because OR is used as the selective condition here, that is, as long as one of the conditions is met), it will call itself recursively, and there will be one less directory in the passed in path. In this way, first go back to the parent directory of the upper level, and then use mkdir to create the next level.<ol class="dp-xml"> <li class="alt"><span><span>function create_folders($dir){ </span></span></li> <li><span>return is_dir($dir) or (<br>create_folders(dirname($dir)) <br>and mkdir($dir, 0777)); </span></li> <li class="alt"><span>} </span></li> </ol>
Okay, the above is how to create folders (and multi-level folders) using PHP. ^_^
Summary of creating folders in PHP:
1 Use a new parameter recursive in mkdir that comes with PHP5. This can be achieved by setting recursive to true The purpose of creating directories recursively, but it does not work for PHP4.
2 In my own method, use mkdirs to create multi-level parent directories
3 Still use mkdir to create it very cleverly,
http://www.bkjia.com/PHPjc/446011.html
www.bkjia.com
http: //www.bkjia.com/PHPjc/446011.html