Why is it important to check file permissions in addition to folder permissions when using PHP fwrite function?

When using the PHP `fwrite` function to write to a file, it is important to check both the file permissions and folder permissions. This is because even if the folder has write permissions, if the file itself does not have the appropriate permissions, the write operation will fail. To ensure successful writing, you should check and set the file permissions accordingly before attempting to write to the file.

$filename = 'example.txt';

// Check and set file permissions
if (!file_exists($filename)) {
    touch($filename);
    chmod($filename, 0644); // Set file permissions to allow writing
}

$file = fopen($filename, 'w');
if ($file) {
    fwrite($file, 'Hello, World!');
    fclose($file);
} else {
    echo 'Failed to open file for writing.';
}