What are the potential pitfalls of creating directories with PHP and how can they be avoided?

One potential pitfall of creating directories with PHP is not properly handling errors that may occur during the process, such as permissions issues or existing directory conflicts. To avoid this, you can use PHP's `mkdir()` function with the `recursive` parameter set to `true` to create directories recursively, ensuring that any necessary parent directories are also created if they do not already exist.

$directory = "path/to/new/directory";

if (!file_exists($directory)) {
    if (!mkdir($directory, 0777, true)) {
        die('Failed to create directory');
    }
} else {
    echo 'Directory already exists';
}