What are some best practices for handling file sizes and creation dates in PHP?

When handling file sizes and creation dates in PHP, it is important to ensure that you are accurately retrieving and displaying this information to users. To handle file sizes, you can use the PHP filesize() function to get the size of a file in bytes and then convert it to a human-readable format. To handle creation dates, you can use the filectime() function to get the creation timestamp of a file and then format it using the date() function.

// Get file size in human-readable format
function formatFileSize($filePath) {
    $size = filesize($filePath);
    $units = array('B', 'KB', 'MB', 'GB', 'TB');

    $i = 0;
    while ($size >= 1024 && $i < count($units) - 1) {
        $size /= 1024;
        $i++;
    }

    return round($size, 2) . ' ' . $units[$i];
}

// Get file creation date in formatted string
function getFileCreationDate($filePath) {
    $timestamp = filectime($filePath);
    return date('Y-m-d H:i:s', $timestamp);
}

// Example usage
$filePath = 'example.txt';
echo 'File Size: ' . formatFileSize($filePath) . PHP_EOL;
echo 'Creation Date: ' . getFileCreationDate($filePath);