What are the best practices for handling and formatting paths in PHP scripts when working with file manipulation?

When working with file manipulation in PHP scripts, it is important to handle and format paths correctly to ensure compatibility across different operating systems. To achieve this, you can use the `DIRECTORY_SEPARATOR` constant to dynamically generate paths and the `realpath()` function to resolve any symbolic links or relative paths.

// Example of handling and formatting paths in PHP scripts for file manipulation
$directory = 'uploads';
$filename = 'example.txt';

// Constructing a path using DIRECTORY_SEPARATOR constant
$fullPath = realpath($directory . DIRECTORY_SEPARATOR . $filename);

// Checking if the file exists before performing any operations
if (file_exists($fullPath)) {
    // Perform file manipulation operations here
    echo "File exists at path: " . $fullPath;
} else {
    echo "File does not exist at path: " . $fullPath;
}