How can one effectively debug PHP scripts that involve file handling functions?

When debugging PHP scripts that involve file handling functions, it is important to check for common errors such as incorrect file paths, incorrect file permissions, or missing file extensions. Using functions like file_exists(), is_readable(), and is_writable() can help diagnose these issues. Additionally, using error_reporting() and error_log() functions can help capture and display any errors that occur during file handling operations.

$file = 'example.txt';

if (file_exists($file) && is_readable($file)) {
    $handle = fopen($file, 'r');
    
    if ($handle) {
        while (($line = fgets($handle)) !== false) {
            echo $line;
        }
        
        fclose($handle);
    } else {
        error_log('Error opening file');
    }
} else {
    error_log('File does not exist or is not readable');
}