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

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

具体代码参考如下:

//获取当前目录
$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>';
    }
}


相关文章
热门推荐