How can the stat() function be utilized in PHP to retrieve information about files, and what are the differences in behavior between different operating systems like Windows and Unix?

The stat() function in PHP can be used to retrieve information about files, such as file size, permissions, and last modification time. However, the behavior of stat() can vary between different operating systems like Windows and Unix. To ensure cross-platform compatibility, it's important to handle these differences appropriately in your code.

$file = 'example.txt';

// Get file information using stat()
$fileInfo = stat($file);

// Check if stat() was successful
if ($fileInfo === false) {
    die('Error getting file information');
}

// Access specific file information
$fileSize = $fileInfo['size'];
$lastModified = $fileInfo['mtime'];

echo "File size: $fileSize bytes\n";
echo "Last modified: " . date('Y-m-d H:i:s', $lastModified) . "\n";