What are the best practices for handling file operations in PHP to avoid errors and ensure efficiency?

When handling file operations in PHP, it is important to check for errors and handle them gracefully to prevent unexpected behavior or security vulnerabilities. To ensure efficiency, use functions like `file_exists()` and `is_readable()` to verify file existence and permissions before attempting to read or write to them. Additionally, always close file handles using `fclose()` after you are done with them to free up system resources.

// Example of checking file existence and readability before performing file operations
$file = 'example.txt';

if (file_exists($file) && is_readable($file)) {
    $handle = fopen($file, 'r');
    
    // Perform file operations here
    
    fclose($handle); // Close file handle to free up system resources
} else {
    echo "Error: File does not exist or is not readable.";
}