What are the potential reasons for receiving a "Permission denied" error when trying to upload files in PHP, and how can this issue be resolved?

The "Permission denied" error in PHP typically occurs when the script does not have the necessary permissions to write to the specified directory. This can be resolved by ensuring that the directory has the correct permissions set to allow the PHP script to write to it.

// Example code to set correct permissions on the directory
$directory = 'uploads/';
if (!is_dir($directory)) {
    mkdir($directory, 0777, true);
}
chmod($directory, 0777);