What are the best practices for handling file and directory manipulation in PHP to avoid server errors?

When handling file and directory manipulation in PHP, it's important to check for errors and handle them gracefully to avoid server errors. One way to do this is by using functions like `file_exists()`, `is_readable()`, `is_writable()`, and `mkdir()` with proper error checking and handling.

// Check if file exists and is readable
if (file_exists($file) && is_readable($file)) {
    // Perform file operations
} else {
    // Handle error
    echo "File does not exist or is not readable.";
}

// Check if directory exists and is writable
if (is_dir($dir) && is_writable($dir)) {
    // Perform directory operations
} else {
    // Handle error
    echo "Directory does not exist or is not writable.";
}

// Create a directory with proper error handling
if (!mkdir($dir, 0777, true)) {
    // Handle error
    echo "Failed to create directory.";
}