Home  >  Article  >  Daily Programming  >  How to create multi-level directories in PHP? (Pictures + Videos)

How to create multi-level directories in PHP? (Pictures + Videos)

藏色散人
藏色散人Original
2018-09-25 16:13:366480browse

This article mainly introduces to you The specific method of creating a multi-level directory in PHP.

During the process of PHP learning or project development, there may be a need to create multi-level directories, which may be difficult for novices. Below we will explain it in detail through specific code examples. We hope it will be helpful to friends in need.

In fact, the main thing you need to understand is a PHP function to create multi-level directories, which is the mkdir function!

The code example of PHP creating a multi-level directory is as follows:

<?php
function create_dir($dirName)
{
    // 去除输入目录名中的空格部分
    $dirName = trim($dirName);
    // 判断输入的目录名称不能为空
    if (empty($dirName)) {
        return "需要创建的目录名称不能为空!";
    } else {
        // 判断是否存在相同文件或目录
        if (file_exists($dirName)) {
            return "已经存在同名目录或文件!";
        } else {
            // 判断并创建目录
            if (mkdir($dirName, 0777,true)) {
                return "目录创建成功!";
            } else {
                return "目录创建失败!";
            }
        }
    }
}

echo create_dir(&#39;index/view&#39;);
?>

Here we define a create_dir method. In this method, first pass the trim function Format the directory name by removing spaces, and then use the if statement to conditionally judge the created directory. Use empty to determine whether the directory is empty and file_exists to determine whether it is empty. If the same directory name exists, you can finally create a multi-level directory through the key mkdir function in PHP.

The mkdir function can be used to create directories.

There are three parameters in mkdir, as in the above code:

mkdir($dirName, 0777,true)

The first parameter indicates the path to create a multi-level directory, and this parameter must exist.

The second parameter indicates the permissions of the set directory. The default is 0777, which means the maximum possible access rights.

The third parameter true indicates that multi-level directories are allowed to be created.

Then the above code passes the browser test, and the result is as shown below:

How to create multi-level directories in PHP? (Pictures + Videos)

Note: If If the directory name you create is empty, the following prompt will appear:

How to create multi-level directories in PHP? (Pictures + Videos)

If you remove the third parameter in mkdir, the following error prompt will appear:

How to create multi-level directories in PHP? (Pictures + Videos)

This article is about the specific method of PHP creating a multi-level directory.

If you want to know more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How to create multi-level directories in PHP? (Pictures + Videos). 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