What is the issue with using fopen to save a file on the server in PHP?

When using fopen to save a file on the server in PHP, one common issue is that the file may not have the correct permissions set, leading to errors when trying to write to the file. To solve this issue, you can explicitly set the file permissions using the chmod function after creating the file with fopen.

$filename = 'example.txt';
$file = fopen($filename, 'w');
if ($file) {
    // Write content to file
    fwrite($file, 'Hello, World!');
    
    // Set file permissions
    chmod($filename, 0644);
    
    fclose($file);
    echo 'File saved successfully.';
} else {
    echo 'Error opening file.';
}