What are best practices for checking directories and files in PHP when developing a filesystem application?

When developing a filesystem application in PHP, it is important to check if directories and files exist before attempting to read or manipulate them. This can help prevent errors and ensure the application runs smoothly. One way to do this is by using PHP's built-in functions such as `file_exists()` and `is_dir()` to check the existence of directories and files.

// Check if a directory exists
$directory = '/path/to/directory';
if (is_dir($directory)) {
    echo 'Directory exists';
} else {
    echo 'Directory does not exist';
}

// Check if a file exists
$file = '/path/to/file.txt';
if (file_exists($file)) {
    echo 'File exists';
} else {
    echo 'File does not exist';
}