Repository navigation
Expand file tree
/
Copy pathFileTools.php
More file actions
85 lines (75 loc) · 2.2 KB
/
Copy pathFileTools.php
File metadata and controls
85 lines (75 loc) · 2.2 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace app\common\support\tools;
/**
* 文件工具。
*
* 封装扩展名、路径规范化、路径安全检查和文件大小格式化。
* Author: qiufeng
*/
trait FileTools
{
/**
* 取文件扩展名(小写,不含点)。
*
* @param string $path 文件名或路径
* @return string 扩展名,如 'jpg';无扩展名返回空串
*/
public static function extension(string $path): string
{
return strtolower(pathinfo($path, PATHINFO_EXTENSION));
}
/**
* 规范化路径:统一为 / 分隔,解析掉 . 与 ..,去除多余分隔符。
*
* @param string $path 原路径
* @return string 规范化后的路径
*/
public static function normalizePath(string $path): string
{
$path = str_replace('\\', '/', $path);
$parts = [];
foreach (explode('/', $path) as $part) {
if ($part === '' || $part === '.') {
continue;
}
if ($part === '..') {
array_pop($parts);
continue;
}
$parts[] = $part;
}
return implode('/', $parts);
}
/**
* 校验路径不含 ..(防目录穿越)。
*
* @param string $path 待校验的路径
* @return string 规范化后的安全路径
* @throws \InvalidArgumentException 路径含 .. 时
*/
public static function assertSafePath(string $path): string
{
if (str_contains($path, '..')) {
throw new \InvalidArgumentException('文件路径不安全');
}
return self::normalizePath($path);
}
/**
* 字节数转人类可读大小,如 1024 → 1 KB。
*
* @param int $bytes 字节数
* @param int $precision 保留的小数位,默认 2
* @return string 带单位的大小(B/KB/MB/GB/TB)
*/
public static function humanSize(int $bytes, int $precision = 2): string
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$index = 0;
$size = $bytes;
while ($size >= 1024 && $index < count($units) - 1) {
$size /= 1024;
$index++;
}
return round($size, $precision) . ' ' . $units[$index];
}
}