To create a new directory in PHP, you can use the mkdir()
function. Here's an example:
<?php
$directoryPath = '/path/to/new/directory';
if (!is_dir($directoryPath)) {
mkdir($directoryPath, 0777, true); // Create the directory with read, write, and execute permissions
echo "Directory created successfully";
} else {
echo "Directory already exists";
}
?>
In this example, you need to specify the desired directory path in the $directoryPath
variable. The is_dir()
function is used to check if the directory already exists. If it doesn't exist, the mkdir()
function is called to create the directory using the specified path. The 0777
parameter is the permission mode for the directory, allowing read, write, and execute permissions for all users. The third parameter is set to true
to create intermediate directories if they don't already exist. Finally, an appropriate message is displayed depending on whether the directory was created successfully or already exists.