In what scenarios would it be advisable to use is_writable() function in PHP file handling operations?
The is_writable() function in PHP can be used to check if a file or directory is writable before attempting to write to it. This can help prevent errors or issues that may occur if the file is not writable.
// Check if a file is writable before writing to it
$file = 'example.txt';
if (is_writable($file)) {
// Open the file for writing
$handle = fopen($file, 'w');
// Write to the file
fwrite($handle, 'Hello, world!');
// Close the file
fclose($handle);
echo 'File written successfully!';
} else {
echo 'File is not writable. Please check file permissions.';
}