Why is it important to use is_readable() and is_writable() functions when working with files in PHP?
It is important to use is_readable() and is_writable() functions when working with files in PHP to ensure that the file exists and can be accessed for reading or writing. These functions help prevent errors by checking the file's permissions before attempting to perform any operations on it.
$file = 'example.txt';
if (is_readable($file)) {
// Read file contents
$contents = file_get_contents($file);
echo $contents;
} else {
echo 'File is not readable';
}
if (is_writable($file)) {
// Write to file
file_put_contents($file, 'New content');
echo 'File written successfully';
} else {
echo 'File is not writable';
}