What is the function of the mkdir function in PHP?

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

Its purpose is to create a new folder on the specified path. If the folder is successfully created, it will return true; if the creation fails, it will return false.

The mkdir function can take two parameters.

  1. The first parameter is the path of the folder to be created (which can be relative or absolute).
  2. The second parameter is optional and is used to set the folder permissions. The default permission is 0777.

Here is an example of creating a folder using the mkdir function.

<?php
$dir = "/path/to/new/folder"; // 要创建的文件夹路径
if (!file_exists($dir)) { // 检查文件夹是否已经存在
    if (mkdir($dir, 0777, true)) { // 创建文件夹
        echo "文件夹创建成功";
    } else {
        echo "文件夹创建失败";
    }
} else {
    echo "文件夹已经存在";
}
?>

In the above example, first check if the folder to be created already exists. If it doesn’t, call the mkdir function to create the folder. If the creation is successful, output “Folder created successfully”; if not, output “Failed to create folder”. If the folder already exists, output “Folder already exists”.

bannerAds