English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Two Methods to Create Multi-level Directories in PHP

PHP has a special function mkdir() for directory creation: returns true on success, returns false on failure

The function mkdir has four parameters:

path: The name of the directory to be created

mode: Permissions. The default is 0777(maximum permissions)

recursive: Set whether multi-level directories can be created (true: can, false: cannot)

context: The environment of the file handle. Context is a set of options that can modify the behavior of a stream (rarely used)

Below, I will introduce two methods for creating multi-level directories in PHP, the specific details are as follows:

1.Using the idea of recursion

function mkdirs_2($path){
if(!is_dir($path)){
mkdirs_2(dirname($path));
if(!mkdir($path, 0777)){
return false;
}
}
return true;
}
/* http://www.manongjc.com/article/1331.html */
$path2 = 'sdfs/sds/sds/s/s/sss';
var_dump(mkdirs_2($path2)) //true;

1.The thought mainly utilizes recursion, first creating 'dir', then creating 'dir'/css, in creating….

2.recursion is the process of pushing into the stack, so let,dir/css/js/php/ok first in stack, then it will be out of stack last...the others will not be mentioned.

3.dirname(path) returns the value of path except the last directory, that is, the first return is: dir/css/js/php,,,the second return is dir/css/js,,,the last one is ./

2.directly use mkdir() to create, the third parameter must be true to automatically create multi-level directories

function mkdirs_1($path, $mode = 0777){
if(is_dir($path)){
return 'Cannot create, it is already a directory';
}else{
if(mkdir($path, $mode, true)) {
return 'Create successfully';
}else{
return 'Create failed';
}
}
}
/* http://www.manongjc.com/article/1332.html */
$path1 = 'a/b/c/d/e';
var_dump(mkdirs_1($path1)) //string 'Create successfully' (length=12)

The two methods of PHP creating multi-level directories introduced by the editor above are for your reference. If you have any questions, please leave a message, and the editor will reply to you in time. We are also very grateful for the support of everyone for the Naoa tutorial website!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting, please replace # with @) for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like