PHP mkdir and mkdirs _php tutorials for directory operations

Source: Internet
Author: User

How to create a multilevel directory using mkdir in PHP

2012-09-09 17:50:58| Category: Default categories

function Mkdirs ($dir)
{
if (!is_dir ($dir))
{
if (! $this->mkdirs (dirname ($dir))) {
return false;
}
if (!mkdir ($dir, 0777)) {
return false;
}
}
chmod ($dir, 777); Permissions for directory Operations
return true;
}

At first I thought that as long as you give a path, mkdir can create folders, but the fact is not that, a single mkdir can only create a level of directory, for the multilevel is not.

So how do you use mkdir to create it? Let me just copy a description of the mkdir in the manual, as follows:

BOOL MkDir (string pathname [, int mode [, bool recursive [, resource context]])


Try creating a new directory that is specified by pathname.

Note that you might want to specify the pattern in octal numbers, which means that the number should begin with 0. The pattern is also modified by the current umask and can be changed with Umask ().


Note: mode is ignored under Windows. Since PHP 4.2.0 has become an option.

The default mode is 0777, which means the maximum possible access rights. For more information on mode, please read the chmod () page. Example 1. mkdir () example

mkdir ("/path/to/my/dir", 0700);
?>


Returns TRUE if successful, and FALSE if it fails.

Note: Since PHP 5.0.0 rmdir () is also available for some URL encapsulation protocols. See the list of appendix N to see what URL encapsulation protocols are supported by RmDir ().
Note: Support for the context is added by PHP 5.0.0. For a description of the context see reference CLX, Stream Functions.
Note: The recursive parameter is added by PHP 5.0.0.
Note: When Safe mode is activated, PHP will check if the directory being manipulated has the same UID (owner) as the script being executed.

The above is the descriptive information in the PHP5 manual, i.e. you can: mkdir ('./test ', 0777) can create a folder. But how do you create a multilevel directory recursively?

The methods are:

1. New method for using PHP5

Create a directory function under PHP5 mkdir adds a new parameter recursive, by setting recursive to true to achieve the purpose of recursively creating a directory, but not for PHP4.

2. Write a recursive create multilevel directory yourself.

Here, I will explain the second way, there are two ways, as follows:

First (use Mkdirs to generate multilevel parent)

function Mkdirs ($dir, $mode = 0777) {if (Is_dir ($dir) | | @mkdir ($DIR, $mode)) return true;if (!mkdirs (DirName ($dir), $mode) ) return False;return @mkdir ($dir, $mode);}

Description

1. First of all, briefly describe the difference between mkdir () and Mkdirs (), and, Is_dir and DirName ():

mkdir () creates a folder (that is, the parent must have a row) only in the directory that already exists.
Mkdirs () can create a folder in a directory that does not exist. such as: a\\b, you can create a multilevel directory.

DirName () is the directory portion of the return path.

Is_dir () is used to determine whether the given file name is a valid directory

2. The approximate process is:
(1) First use Is_dir to determine if it is already a folder, and if so, return true. If there is no (or not a folder), then try to create it, of course, there can be a parent does not exist, that mkdir directly on the creation is not successful, but also for not error, so use @ To suppress the report non-fatal error.

(2) If the parameters do not meet the conditions, then enter the second if statement, first get the directory part of the path, of course, there may be no multi-level parent, so use mkdirs () to create the parent first, if successful (not successfully return false), then use mkdir to create the final directory.

Well, that's the first option.

The second Kind

(Description: This scenario is quite streamlined, that's quite a good solution, recommended to use it)

function Create_folders ($dir) {return Is_dir ($dir) or (Create_folders (dirname ($dir)) and mkdir ($dir, 0777));}

Description

approximate process: after obtaining the path, first determine whether it is a valid file directory, if it is returned, the end of the program. If it is not, (because it uses or for the first choice, that is, as long as one of the conditions is satisfied), then recursively calls itself, and the incoming path, less a level of directory. This is to go back to the parent directory, and then use mkdir to create the next level.

Well, here's how to create folders (and multilevel folders) with PHP. ^_^

Summarize:

1. Using a new parameter from the mkdir in the PHP5 recursive, it is possible to recursively create the directory by setting the recursive to true, but not for PHP4.

2. Use Mkdirs to create multi-level parent catalogs in your own way

3. You can still use mkdir to create multi-level folders.

http://www.bkjia.com/PHPjc/635024.html www.bkjia.com true http://www.bkjia.com/PHPjc/635024.html techarticle methods for creating multilevel catalogs using mkdir in PHP 2012-09-09 17:50:58| classification: Default classification function mkdirs ($dir) {if (!is_dir ($dir)) {if (! $this->mkdirs ( DirName ($dir))) {return False ...

  • Related Article

    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.