-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileHelper.php
40 lines (35 loc) · 1.24 KB
/
FileHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
class FileHelper {
public static $file_units = array(
'GB' => 'ГБ',
'MB' => 'МБ',
'KB' => 'КБ',
'bytes' => 'байт'
);
/**
* @param $size
* @param string|null $unit
* @see https://stackoverflow.com/questions/15188033/human-readable-file-size
*
* @return string
*/
public static function humanFileSize($size, string $unit = null): string
{
if (( ! $unit && $size >= 1 << 30) || $unit === "GB") {
return number_format($size / (1 << 30), 2)." ГБ" . self::$file_units['GB'];
}
if (( ! $unit && $size >= 1 << 20) || $unit === "MB") {
return number_format($size / (1 << 20), 2)." " . self::$file_units['MB'];
}
if (( ! $unit && $size >= 1 << 10) || $unit === "KB") {
return number_format($size / (1 << 10), 2)." " . self::$file_units['KB'];
}
return number_format($size)." " . self::$file_units['bytes'];
}
public static function sanitizeFilename(string $string)
{
$bad = ["<", ">", ":", '"', "/", "\\", "|", "?", "*"];
$bad = array_merge($bad, array_map('chr', range(0,31)));
return str_replace($bad, "", $string);
}
}