1 | <?php |
||
2 | |||
3 | // +---------------------------------------------------------------------- |
||
4 | // | ThinkLibrary 6.0 for ThinkPhP 6.0 |
||
5 | // +---------------------------------------------------------------------- |
||
6 | // | 版权所有 2017~2020 [ https://www.dtapp.net ] |
||
7 | // +---------------------------------------------------------------------- |
||
8 | // | 官方网站: https://gitee.com/liguangchun/ThinkLibrary |
||
9 | // +---------------------------------------------------------------------- |
||
10 | // | 开源协议 ( https://mit-license.org ) |
||
11 | // +---------------------------------------------------------------------- |
||
12 | // | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary |
||
13 | // | github 仓库地址 :https://github.com/GC0202/ThinkLibrary |
||
14 | // | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library |
||
15 | // +---------------------------------------------------------------------- |
||
16 | |||
17 | declare (strict_types=1); |
||
18 | |||
19 | namespace DtApp\ThinkLibrary\helper; |
||
20 | |||
21 | use DtApp\ThinkLibrary\exception\DtaException; |
||
22 | use think\Exception; |
||
23 | use ZipArchive; |
||
24 | |||
25 | /** |
||
26 | * 文件管理类 |
||
27 | * @mixin Files |
||
28 | * @package DtApp\ThinkLibrary\helper |
||
29 | */ |
||
30 | class Files |
||
31 | { |
||
32 | /** |
||
33 | * 删除文件 |
||
34 | * @param string $name 路径 |
||
35 | * @return bool |
||
36 | * @throws DtaException |
||
37 | */ |
||
38 | public function delete(string $name): bool |
||
39 | { |
||
40 | if (empty($name)) { |
||
41 | throw new DtaException('请检查需要删除文件夹的名称'); |
||
42 | } |
||
43 | return file_exists($name) && unlink($name); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * 删除文件夹 |
||
48 | * @param string $name 路径 |
||
49 | * @return bool |
||
50 | * @throws DtaException |
||
51 | * @throws Exception |
||
52 | */ |
||
53 | public function deletes(string $name): bool |
||
54 | { |
||
55 | if (empty($name)) { |
||
56 | throw new DtaException('请检查需要删除文件夹的名称'); |
||
57 | } |
||
58 | //先删除目录下的文件: |
||
59 | $dh = opendir($name); |
||
60 | while ($file = readdir($dh)) { |
||
61 | if ($file !== "." && $file !== "..") { |
||
62 | $fullpath = $name . "/" . $file; |
||
63 | if (!is_dir($fullpath)) { |
||
64 | unlink($fullpath); |
||
65 | } else { |
||
66 | $this->deletes($fullpath); |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | closedir($dh); |
||
71 | //删除当前文件夹: |
||
72 | if (rmdir($name)) { |
||
73 | return true; |
||
74 | } |
||
75 | |||
76 | return false; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * 把文件夹里面的文件打包成zip文件 |
||
81 | * @param string $name 路径 |
||
82 | * @param string $suffix_name 需要打包的后缀名,默认.png |
||
83 | * @param string $file_name 文件名,默认全部名 |
||
84 | * @return bool |
||
85 | * @throws DtaException |
||
86 | */ |
||
87 | public function folderZip(string $name, string $suffix_name = '.png', string $file_name = '*'): bool |
||
88 | { |
||
89 | if (empty($name)) { |
||
90 | throw new DtaException('请检查需要打包的路径名称'); |
||
91 | } |
||
92 | $list = glob($name . "{$file_name}.{$suffix_name}"); |
||
93 | $fileList = $list; |
||
94 | $zip = new ZipArchive(); |
||
95 | // 打开压缩包 |
||
96 | $zip->open($name, ZipArchive::CREATE); |
||
97 | //向压缩包中添加文件 |
||
98 | foreach ($fileList as $file) { |
||
99 | $zip->addFile($file, basename($file)); |
||
100 | } |
||
101 | //关闭压缩包 |
||
102 | $zip->close(); |
||
103 | return true; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * 获取目录下的所有文件和目录 |
||
108 | * @param string $path |
||
109 | * @return array |
||
110 | */ |
||
111 | public function getFiles(string $path): array |
||
112 | { |
||
113 | $files = []; |
||
114 | if (is_dir($path)) { |
||
115 | $path = dirname($path) . '/' . basename($path) . '/'; |
||
116 | $file = dir($path); |
||
117 | while (false !== ($entry = $file->read())) { |
||
118 | if ($entry !== '.' && $entry !== '..') { |
||
119 | $cur = $path . $entry; |
||
120 | if (is_dir($cur)) { |
||
121 | $subPath = $cur . '/'; |
||
122 | $this->getFiles($subPath); |
||
123 | } |
||
124 | $files[] = $cur; |
||
125 | } |
||
126 | } |
||
127 | $file->close(); |
||
128 | return $files; |
||
129 | } |
||
130 | |||
131 | return []; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * 删除目录下的文件 |
||
136 | * @param string $path |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function rmFiles(string $path): bool |
||
140 | { |
||
141 | $files = $this->getFiles($path); |
||
142 | if (!is_array($files)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
143 | return false; |
||
144 | } |
||
145 | |||
146 | if (empty($files)) { |
||
147 | return false; |
||
148 | } |
||
149 | |||
150 | foreach ($files as $item => $file) { |
||
151 | if (is_dir($file)) { |
||
152 | rmdir($file); |
||
153 | } elseif (is_file($file)) { |
||
154 | unlink($file); |
||
155 | } |
||
156 | } |
||
157 | return true; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * 判断文件是否存在 |
||
162 | * @param string $path |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function judgeFile(string $path): bool |
||
166 | { |
||
167 | if (file_exists($path)) { |
||
168 | return true; |
||
169 | } |
||
170 | return false; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * 判断目录是否存在 |
||
175 | * @param string $path |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function judgeContents(string $path): bool |
||
179 | { |
||
180 | if (is_dir($path)) { |
||
181 | return true; |
||
182 | } |
||
183 | return false; |
||
184 | } |
||
185 | } |
||
186 |