What are the best practices for handling file paths in PHP to avoid errors like "supplied argument is not a valid stream resource"?

When working with file paths in PHP, it's important to ensure that the paths are valid and properly formatted to avoid errors like "supplied argument is not a valid stream resource." To handle file paths correctly, you should use the `realpath()` function to get the absolute path of a file and check if the file exists before performing any operations on it.

$path = '/path/to/file.txt';

// Get the absolute path of the file
$realPath = realpath($path);

// Check if the file exists
if ($realPath && file_exists($realPath)) {
    // Perform operations on the file
    $fileContents = file_get_contents($realPath);
    echo $fileContents;
} else {
    echo "File does not exist or is not accessible.";
}