What are the basic arithmetic operations that can be used to convert file sizes from bytes to KB or MB in PHP?

To convert file sizes from bytes to KB or MB in PHP, we can use basic arithmetic operations such as division and multiplication. To convert bytes to KB, we divide the size by 1024. To convert bytes to MB, we divide the size by 1024 * 1024.

function convertBytes($bytes) {
    $kb = $bytes / 1024;
    $mb = $bytes / (1024 * 1024);

    return array(
        'bytes' => $bytes,
        'kilobytes' => $kb,
        'megabytes' => $mb
    );
}

$fileSize = 1024; // Size in bytes
$convertedSizes = convertBytes($fileSize);

echo "File size: {$convertedSizes['bytes']} bytes\n";
echo "File size: {$convertedSizes['kilobytes']} KB\n";
echo "File size: {$convertedSizes['megabytes']} MB\n";