Two ways to create multilevel directories in PHP _php instance

Source: Internet
Author: User
Tags mkdir

PHP for directory creation, there is a special function mkdir (): Success, return True, failed, return false

The function mkdir has four parameters:

Path: The name of the directory to create

Mode: Permissions. Default is 0777 (maximum permissions)

Recursive: Sets whether a multilevel directory can be created (true: Can, false: not)

Context: The environment for file handles. Context is a set of options (rarely used) to modify the behavior of a stream

Here are two ways to introduce PHP to create a multilevel directory, the details are as follows:

1. The idea of using 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 * *
$path 2 = ' sdfs/sds/sds/s/s/sss ';
Var_dump (Mkdirs_2 ($path 2)); True

1. The idea is primarily to use recursion, first to create DIR, to create DIR/CSS, to create ....

2. Recursion is the process of the stack, so first let, Dir/css/js/php/ok first into the stack, then it will be the last stack ... The rest will not be spoken.

3.dirname (path) returns the value of path in addition to the last directory, that is, the first return is: dir/css/js/php,,, return to Dir/css/js for the second time,,, the last time.

2. Directly using mkdir () to create, the third parameter must be true to automatically create multilevel catalogs

function Mkdirs_1 ($path, $mode = 0777) {
if (Is_dir ($path)) {return
' cannot be created, is already a directory ';
} else{
if (mkdir ($path, $mode, True)) {return
' create success ';
} else{return
' Create failed ';
}} /* http://www.manongjc.com/article/1332.html * *
$path 1 = ' a/b/c/d/e ';
Var_dump (Mkdirs_1 ($path 1)); String ' Create Success ' (LENGTH=12)

The above is a small set to introduce PHP to create a multi-level directory of two methods, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.