What are the potential pitfalls of using fopen to access files in PHP, especially in regards to server restrictions on file access?
When using fopen to access files in PHP, one potential pitfall is that the server may have restrictions on file access, leading to permission denied errors. To avoid this issue, it's important to check if the file exists and if the server has the necessary permissions to access it before attempting to open it.
$filename = 'example.txt';
if (file_exists($filename) && is_readable($filename)) {
$file = fopen($filename, 'r');
// Read or write operations can be performed here
fclose($file);
} else {
echo "File does not exist or is not readable.";
}