Home  >  Article  >  Backend Development  >  The mkdir() function in PHP is used to create directories

The mkdir() function in PHP is used to create directories

PHPz
PHPzOriginal
2023-11-18 17:35:171188browse

The mkdir() function in PHP is used to create directories

The mkdir() function in PHP is used to create a directory. A specific code example will be given below.

First of all, we need to understand the basic usage of the mkdir() function. The prototype of the mkdir() function is:

bool mkdir(string $directory, int $mode = 0777, bool $recursive = false, resource $context = null)

Parameter description:

  • $directory: The directory path to be created, which can be an absolute path or a relative path.
  • $mode: Permission to create a directory, the default is 0777, which is the highest permission.
  • $recursive: If set to true, parent directories will be created recursively when creating a directory. Default is false.
  • $context: Optional parameter, used to set the directory context.

The following is a specific code example:

// Create directory
$dir = "/path/to/directory"; / / Set the directory path to be created

if (!file_exists($dir)) { // Determine whether the directory already exists

if (mkdir($dir, 0777, true)) {  // 使用 mkdir() 函数创建目录
    echo "目录创建成功!";
} else {
    echo "目录创建失败!";
}

} else {

echo "目录已存在!";

}
?>

Note:

  • Before using the mkdir() function to create a directory, make sure that the parent directory where the directory is located already exists.
  • If the directory is created successfully, the function will return true; if the creation fails, the function will return false. You can determine whether the creation is successful by returning the value.

The above is a simple code example, you can modify and extend it according to your needs. Hope it helps you!

The above is the detailed content of The mkdir() function in PHP is used to create directories. 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