Tutorial on creating a folder in PHP

Source: Internet
Author: User
Basic tutorial on creating folders in PHP. Before getting started, I would like to explain that many of my friends may think that as long as a path is provided, mkdir can be used to create folders. Otherwise, only one level of MKDIR can be created.

Before getting started, I would like to explain that many of my friends may think that as long as a path is provided, mkdir can be used to create folders. Otherwise, a single MKDIR can only create a level-1 Directory, it won't work for multiple levels. how can we use mkdir to create one? First, I copied a description of mkdir in the manual, as shown below:

Description
Bool mkdir (string pathname [, int mode [, bool recursive [, resource context])

Create a directory specified by pathname.

Note that you may want to specify the eight-digit number mode, that is, the number should start with zero. The mode will also be modified by the current umask, which can be changed by umask.

Note: mode is ignored in Windows. It is optional since PHP 4.2.0.

The default mode is 0777, which means the maximum possible access. For more information about mode, see the chmod () page. Example 1. mkdir ()

 
 
  1. < ?php
  2. mkdir("/path/
    to/my/dir", 0700);
  3. ?>

If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.

Note: PHP 5.0.0 rmdir () can also be used for some URL encapsulation protocols. See the list in Appendix N to see which URL encapsulation protocols are supported by rmdir.
Note: The support for context is added in PHP 5.0.0. For more information about context, see CLX and Stream Functions.
Note: The recursive parameter is added in PHP 5.0.0.
Note: When security mode is activated, PHP checks whether the operated directory has the same UID (owner) as the script being executed ).

The above is the description in the PHP5 manual, that is, you can create a folder in mkdir ('./test', 0777. But how do I recursively create multi-level directories?

PHP creates folders in the following ways:

1. a new recursive parameter is added to the directory creation function mkdir in PHP5. you can recursively create a directory by setting recursive to true, but it won't work for PHP4.

2. write a recursive multi-level Directory.

Here, I will explain the second method in two ways:

First PHP creates a folder (using mkdirs to generate multi-level parent level)

 
 
  1. function mkdirs($dir, $mode = 0777)
  2. {
  3. if (is_dir($dir) ||
    @mkdir($dir, $mode))
    return TRUE;
  4. if (!mkdirs(dirname
    ($dir), $mode)) return FALSE;
  5. return @mkdir($dir, $mode);
  6. }

Note: 1 First, let's briefly talk about the differences between mkdir () and mkdirs (), and is_dir and dirname ():

Mkdir ()
You can only create folders in an existing directory (that is, the parent level must be available ).
Mkdirs ()
You can create folders in a directory that does not exist. For example, a \ B can create multi-level directories.

Dirname () is the directory part of the returned path.

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

2. the general process is as follows: (1) use is_dir to determine whether a folder is already in use. If yes, TRUE is returned. If there is no (or a folder), try to create it. of course, if the parent level does not exist, then mkdir cannot be created directly, but it does not report an error, therefore, use @ to prevent non-fatal errors.

(2) if the passing parameters do not meet the conditions, enter the second if statement. first, obtain the directory part in the path. of course, there may be non-existing multi-level parent level, therefore, mkdirs () is used to create the parent level first. if the parent level is successfully created (FALSE is returned if the parent level is unsuccessful), mkdir is used to create the final directory.

Well, the above is the first solution.

Method 2: create a folder in PHP

(Note: this solution is quite streamlined. it is quite a good solution and is recommended)

 
 
  1. function create_folders($dir){
  2. return is_dir($dir) or (
    create_folders(dirname($dir))
    and mkdir($dir, 0777));
  3. }

Note:

1. general process: after obtaining the path, first determine whether it is a valid file directory. if so, return and end the program. If not, (because the OR condition is used as the first choice, that is, as long as one of the conditions is met), the system will call itself recursively and pass in the path, lower-level Directory. In this way, first go back to the parent directory of the upper level, and then use mkdir to create the next level.

Well, the above is how to use PHP to create folders (and multi-level folders. Pai_^

PHP folder creation summary:

1. Use a new parameter recursive in mkdir in PHP5. you can recursively create a directory by setting recursive to true, but it won't work for PHP4.

2. in the self-written method, mkdirs is used to create a multi-level parent directory.

3. mkdir is still cleverly created,


...

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.