What are some best practices for handling file sizes and storage calculations in PHP?

When handling file sizes and storage calculations in PHP, it is important to properly format and display file sizes in a human-readable format (e.g., KB, MB, GB) and accurately calculate storage requirements for files. One common approach is to use PHP functions like `filesize()` to get the size of a file in bytes and then convert it to the appropriate unit (KB, MB, GB) for display or calculation.

// Function to format file size in human-readable format
function formatFileSize($size) {
    $units = array('B', 'KB', 'MB', 'GB', 'TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) {
        $size /= 1024;
    }
    return round($size, 2) . ' ' . $units[$i];
}

// Example of using the function to format file size
$fileSize = filesize('example.txt');
echo 'File size: ' . formatFileSize($fileSize);