How can the is_writable function in PHP be utilized to prevent blind writing to files with fwrite?

Blind writing to files with fwrite can be prevented by using the is_writable function in PHP to check if a file is writable before attempting to write to it. This function can be used to verify the file's permissions and avoid potential errors that may occur if the file is not writable.

$file = 'example.txt';

if (is_writable($file)) {
    $handle = fopen($file, 'a');
    fwrite($handle, 'Data to be written');
    fclose($handle);
    echo 'Data has been written to the file.';
} else {
    echo 'File is not writable.';
}