-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileUtil.php
More file actions
61 lines (55 loc) · 1.84 KB
/
FileUtil.php
File metadata and controls
61 lines (55 loc) · 1.84 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
<?php
namespace app\utils;
class FileUtil
{
/**
* 删除当前目录及其目录下的所有目录和文件
* @param string $path 待删除的目录
* @note $path路径结尾不要有斜杠/(例如:正确[$path='./linuxidc/image'],错误[$path='./linuxidc/image/'])
*/
function deleteDir($path)
{
if (is_dir($path)) {
//扫描一个目录内的所有目录和文件并返回数组
$dirs = scandir($path);
foreach ($dirs as $dir) {
//排除目录中的当前目录(.)和上一级目录(..)
if ($dir != '.' && $dir != '..') {
//如果是目录则递归子目录,继续操作
$sonDir = $path . '/' . $dir;
if (is_dir($sonDir)) {
//递归删除
$this->deleteDir($sonDir);
//目录内的子目录和文件删除后删除空目录
@rmdir($sonDir);
} else {
//如果是文件直接删除
@unlink($sonDir);
}
}
}
@rmdir($path);
}
}
/**
* 创建zip文档
* @param $destinationZip
* @param $sourceFileArr
* @return bool
*/
public function createZipByFiles($destinationZip, $sourceFileArr)
{
// 创建新的 ZipArchive 对象
$zip = new \ZipArchive();
if ($zip->open($destinationZip, \ZipArchive::CREATE) === true) {
// 添加源文件到压缩包
foreach ($sourceFileArr as $filePath) {
$zip->addFile($filePath, basename($filePath));
}
$zip->close(); // 关闭压缩包
return true;
} else {
return false;
}
}
}