refactor: 修改上传限制可配置

This commit is contained in:
2025-04-11 09:41:48 +08:00
parent c73981344f
commit 36f4e317cf
3 changed files with 47 additions and 3 deletions

View File

@@ -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;
}
}