What are common reasons for the PHP error "Permission denied" when trying to open a file?

The "Permission denied" error in PHP typically occurs when the script does not have the necessary permissions to access the file. This can happen if the file's permissions are set to restrict access by the PHP script. To solve this issue, you can either change the file's permissions to allow access by the PHP script or adjust the permissions of the directory containing the file.

// Example code snippet to set file permissions to allow access by PHP script
$file = 'example.txt';
chmod($file, 0644);