如何用PHP代码实现zip文件的解压操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计268个文字,预计阅读时间需要2分钟。
使用PHP解压缩zip文件:
1.确保服务器已开启zip扩展。
2.封装方法如下:
phpopen('archive.zip', ZipArchive::CREATE) !==TRUE) { return false; } foreach ($files as $file) { $zip->addFile($file); } $zip->close(); return true;}?>PHP解压缩zip文件
1、使用PHP执行文件解压缩zip文件,前提条件,一定要确定服务器开启了zip拓展
2、封装的方法如下:
实例代码
<?php /** * 压缩文件 * @param array $files 待压缩文件 array('d:/test/1.txt','d:/test/2.jpg'); * @param string $filePath 输出文件路径 如 d:/test/new.zip * @return string|bool */ function zip($files, $filePath) { //检查参数 if (empty($files) || empty($filePath)) { return false; } //压缩文件 $zip = new ZipArchive(); $zip->open($filePath, ZipArchive::CREATE); foreach ($files as $key => $file) { //检查文件是否存在 if (!file_exists($file)) { return false; } $zip->addFile($file, basename($file)); } $zip->close(); return true; } /** * zip解压方法 * @param string $filePath 压缩包所在地址 d:/test/123.zip * @param string $path 解压路径 d:/test * @return bool */ function unzip($filePath, $path) { if (empty($path) || empty($filePath)) { return false; } $zip = new ZipArchive(); if ($zip->open($filePath) === true) { $zip->extractTo($path); $zip->close(); return true; } else { return false; } } ?>
相关教程
本文共计268个文字,预计阅读时间需要2分钟。
使用PHP解压缩zip文件:
1.确保服务器已开启zip扩展。
2.封装方法如下:
phpopen('archive.zip', ZipArchive::CREATE) !==TRUE) { return false; } foreach ($files as $file) { $zip->addFile($file); } $zip->close(); return true;}?>PHP解压缩zip文件
1、使用PHP执行文件解压缩zip文件,前提条件,一定要确定服务器开启了zip拓展
2、封装的方法如下:
实例代码
<?php /** * 压缩文件 * @param array $files 待压缩文件 array('d:/test/1.txt','d:/test/2.jpg'); * @param string $filePath 输出文件路径 如 d:/test/new.zip * @return string|bool */ function zip($files, $filePath) { //检查参数 if (empty($files) || empty($filePath)) { return false; } //压缩文件 $zip = new ZipArchive(); $zip->open($filePath, ZipArchive::CREATE); foreach ($files as $key => $file) { //检查文件是否存在 if (!file_exists($file)) { return false; } $zip->addFile($file, basename($file)); } $zip->close(); return true; } /** * zip解压方法 * @param string $filePath 压缩包所在地址 d:/test/123.zip * @param string $path 解压路径 d:/test * @return bool */ function unzip($filePath, $path) { if (empty($path) || empty($filePath)) { return false; } $zip = new ZipArchive(); if ($zip->open($filePath) === true) { $zip->extractTo($path); $zip->close(); return true; } else { return false; } } ?>
相关教程

