How can PHP scripts handle file paths when running on different operating systems, such as Windows and Linux?

PHP scripts can handle file paths on different operating systems by using the built-in DIRECTORY_SEPARATOR constant to represent the correct directory separator for the current operating system. This allows for dynamic creation of file paths that are compatible across platforms. Additionally, the realpath() function can be used to normalize file paths and ensure they are in the correct format for the operating system.

// Example of handling file paths for different operating systems
$basePath = 'path/to/files';
$fileName = 'example.txt';

// Create a file path using DIRECTORY_SEPARATOR constant
$filePath = $basePath . DIRECTORY_SEPARATOR . $fileName;

// Normalize the file path using realpath()
$normalizedPath = realpath($filePath);

echo $normalizedPath;