refactor: 修改上传限制可配置
This commit is contained in:
@@ -94,4 +94,42 @@ if (!function_exists('xlsx_writer')) {
|
||||
]);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('strtobytes')) {
|
||||
|
||||
/**
|
||||
* 字符串转字节
|
||||
* @param string $str_size 字符串 例:'1MB' '2GB'
|
||||
* @return int
|
||||
*/
|
||||
function strtobytes(string $str_size): int
|
||||
{
|
||||
preg_match('/(\d+)(\w+)/', $str_size, $matches);
|
||||
if (!isset($matches[1])) {
|
||||
throw new \Exception('请确认size大小');
|
||||
}
|
||||
if (!isset($matches[2])) {
|
||||
throw new \Exception('请确认单位');
|
||||
}
|
||||
|
||||
$size = intval($matches[1]);
|
||||
$unit = $matches[2];
|
||||
switch (strtolower($unit)) {
|
||||
case 'kb':
|
||||
$size *= 1024;
|
||||
break;
|
||||
case 'mb':
|
||||
$size *= 1024 * 1024;
|
||||
break;
|
||||
case 'gb':
|
||||
$size *= 1024 * 1024 * 1024;
|
||||
break;
|
||||
case 'tb':
|
||||
$size *= 1024 * 1024 * 1024 * 1024;
|
||||
default:
|
||||
throw new \Exception('不支持[' . $unit . ']单位');
|
||||
}
|
||||
return $size;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user