Home  >  Article  >  Backend Development  >  Introduction to PHP functions—mkdir(): Create directory

Introduction to PHP functions—mkdir(): Create directory

PHPz
PHPzOriginal
2023-07-24 08:29:133025browse

PHP function introduction—mkdir(): Create directory

Introduction:
In web development, sometimes we need to dynamically create directories on the server to store files uploaded by users, temporary files or other data. PHP's mkdir() function is designed to facilitate us to create directories on the server. This article will introduce the usage and code examples of the mkdir() function.

1. Overview of mkdir() function:
The mkdir() function is used to create a directory on the specified path. Its parameters are as follows:

  1. path: The directory path to be created.
  2. mode: Optional parameter, used to specify the permissions of the new directory, the default is 0777 (maximum permissions).
  3. recursive: Optional parameter, specifies whether to create directories recursively. Default is false.

2. mkdir() function example:
The following are several example codes for using the mkdir() function to create a directory:

  1. Simple directory Create:

    <?php
    $dir = 'path/to/new/directory';
    if (!is_dir($dir)) {
     mkdir($dir);
     echo '目录创建成功!';
    } else {
     echo '目录已存在!';
    }
    ?>
  2. Set directory permissions:

    <?php
    $dir = 'path/to/new/directory';
    $mode = 0755;
    if (!is_dir($dir)) {
     mkdir($dir, $mode);
     echo '目录创建成功!';
    } else {
     echo '目录已存在!';
    }
    ?>
  3. Create directory recursively:

    <?php
    $dir = 'path/to/new/recursive/directory';
    if (!is_dir($dir)) {
     mkdir($dir, 0777, true);
     echo '目录创建成功!';
    } else {
     echo '目录已存在!';
    }
    ?>
  4. Dynamic Create a directory:

    <?php
    $dir = 'path/to/new/' . date("Y/m/d/");
    if (!is_dir($dir)) {
     mkdir($dir, 0777, true);
     echo '目录创建成功!';
    } else {
     echo '目录已存在!';
    }
    ?>

    In the above example code, we have implemented functions such as simple directory creation, setting directory permissions, recursively creating directories, and dynamically creating directories. In actual applications, you can choose a suitable directory creation method according to your own needs.

Summary:
Through the introduction of this article, we have learned about the usage of the mkdir() function in PHP and common sample codes. Use the mkdir() function to easily create a directory on the server and set permissions for it. In actual development, we can flexibly use the mkdir() function to meet our directory creation needs based on specific needs and permission settings of the code running environment. Hope this article helps you!

The above is the detailed content of Introduction to PHP functions—mkdir(): Create directory. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn