What are common reasons for "Permission denied" errors when using mkdir in PHP?

When encountering "Permission denied" errors while using mkdir in PHP, it is likely due to the script lacking the necessary permissions to create a directory in the specified location. This can be resolved by ensuring that the directory path exists and that the PHP script has the appropriate permissions to create directories in that location.

$dir = 'path/to/new/directory';

// Check if directory exists, if not, create it
if (!file_exists($dir) && !is_dir($dir)) {
    mkdir($dir, 0777, true);
}