PHP实现ZIP文件自动解压与文件遍历

      发布在:后端技术      评论:0 条评论

具体代码参考如下:

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
//获取当前目录
$dir = dirname(__FILE__);
// 压缩文件
$temp_file=  $dir.'/web.zip';
// 解压文件
$zip new ZipArchive();
if ($zip->open($temp_file) !== TRUE) {
    throw new Exception('无法打开压缩文件');
}
 
// 创建解压目录
$extract_path =  $dir;
 
 
// 执行解压
if (!$zip->extractTo($extract_path)) {
    throw new Exception('解压失败');
}
$zip->close();
 
// 获取解压文件列表
$files new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($extract_path),
    RecursiveIteratorIterator::LEAVES_ONLY
);
 
// 显示结果
echo '<h3>解压成功!文件列表:</h3>';
foreach ($files as $file) {
    if (!$file->isDir()) {
        echo $file->getPathname() . '<br>';
    }
}


阿里云服务器1核/2G/1M(88元/1年) 【腾讯云】云产品限时秒杀,爆款2核4G云服务器首年74元 腾讯云服务器老用户专享(每日限量,下手要快) 香港高稳定云服务器推荐(优惠码:haixiuge)
相关文章
热门推荐