天天看點

PHP檔案大小格式化函數

<?php
/**
 *  容量格式化
 *   Recoded By Androidyue
 * @param String   檔案名(檔案路徑)
 * @return  如果檔案存在傳回格式化的字元串 如果錯誤傳回錯誤資訊  Unknown File
 */
function sizeFormat ($fileName){
//擷取檔案的大小
@ $filesize=filesize($fileName);
//如果檔案不存在傳回錯誤資訊
if(false==$filesize){
return 'Unknown File';
}
//格式化檔案容量資訊
    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 . ' Bytes';
    return $filesize;
}
//測試函數
echo sizeFormat('config.inc.php');
?>