What are the best practices for setting file permissions in PHP to avoid "Permission denied" errors when working with files?
When working with files in PHP, it is important to set the correct file permissions to avoid "Permission denied" errors. To ensure that the PHP script has the necessary permissions to read, write, and execute files, you can use the chmod() function to set the appropriate permissions. It is recommended to set file permissions to 0644 for files and 0755 for directories to prevent permission errors.
// Set file permissions to 0644 for files and 0755 for directories
$filename = 'example.txt';
if (file_exists($filename)) {
chmod($filename, 0644);
echo "File permissions set successfully.";
} else {
echo "File does not exist.";
}