经测试代码如下:
/**
* 带单位的文件大小字符串转换成字节数
*
* @param
* @arrange (512.笔记) jb51.cc
**/
function StringSizeToBytes($Size){
$Unit = strtolower($Size);
$Unit = preg_replace('/[^a-z]/','',$Unit);
$Value = intval(preg_replace('/[^0-9]/',$Size));
$Units = array('b'=>0,'kb'=>1,'mb'=>2,'gb'=>3,'tb'=>4);
$Exponent = isset($Units[$Unit]) ? $Units[$Unit] : 0;
return ($Value * pow(1024,$Exponent));
}
// Example usage:
// Check if a file is bigger than 10 MB
if (filesize('example.zip') > StringSizeToBytes('10 MB')){
print 'File is to big !';
}
else {
print 'File is okay';
}
/*** 来自编程之家 jb51.cc(jb51.cc) ***/