PHP头条
热点:

PHP获取文件大小并转化为KB、MB、GB单位


PHP获取文件大小并转化为KB、MB、GB单位。

function getSize($filesize) {
    if ($filesize >= 1073741824) {
        $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
    } elseif ($filesize >= 1048576) {
        $filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
    } elseif ($filesize >= 1024) {
        $filesize = round($filesize / 1024 * 100) / 100 . ' KB';
    } else {
        $filesize = $filesize . ' 字节';
    }

    return $filesize;
}

或:

function formatBytes($size) {
    $units = [' B', ' KB', ' MB', ' GB', ' TB'];
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return round($size, 2) . $units[$i];
}

echo formatBytes(310258); // 302.99 KB

www.phpzy.comtrue/php/34422.htmlTechArticlePHP获取文件大小并转化为KB、MB、GB单位 PHP获取文件大小并转化为KB、MB、GB单位。 function getSize($filesize) { if ($filesize = 1073741824) { $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB'; } elseif (...

相关文章

PHP之友评论

今天推荐