What are the best practices for handling file paths in PHP?

When working with file paths in PHP, it is important to use the correct functions and methods to ensure cross-platform compatibility. Avoid hardcoding file paths and instead use built-in PHP functions like `realpath()` and `dirname()` to manipulate and sanitize file paths. Additionally, always use the DIRECTORY_SEPARATOR constant when constructing file paths to ensure they work on both Windows and Unix-based systems.

// Example of handling file paths in PHP

// Define the base directory
$baseDir = __DIR__;

// Construct a file path using DIRECTORY_SEPARATOR
$filePath = $baseDir . DIRECTORY_SEPARATOR . 'file.txt';

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

// Get the directory of the file
$directory = dirname($filePath);

// Output the results
echo "File path: $filePath\n";
echo "Real path: $realPath\n";
echo "Directory: $directory\n";