项目涉及文档处理,用户上传的包括 zip
和 rar
压缩包,需要先将压缩包解压后再作处理。对于 zip
压缩包,由于 php
自带 zip
扩展,可以直接解压。
$file = "/opt/data/upload/testfile.zip";
$outPath = "/opt/data/upload/testfile";
$zip = new ZipArchive();
$openRes = $zip->open($file);
if ($openRes === TRUE) {
$zip->extractTo($outPath);
$zip->close();
}
对于 rar
压缩包,需要先为 php
安装 rar
扩展。
wget http://pecl.php.net/get/rar-4.0.0.tgz
gunzip rar-4.0.0.tgz
tar -xvf rar-4.0.0.tar
cd rar-4.0.0
phpize
./configure && make && make install
# 报错
configure: error: Cannot find php-config. Please use --with-php-config=PATH
# 运行./configure 时指定php-config路径即可
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
# 新建 /usr/local/php/conf.d/rar.ini,内容
extension=rar.so
重启 php-fpm
,看一下 phpinfo()
;
可以看到已经成功安装了 rar
,可以来测试一下解压 rar
文件。
$file = "/opt/data/upload/testfile.zip";
$outPath = "/opt/data/upload/testfile";
$rar_file = rar_open($file);
if ($rar_file) {
$entries = rar_list($rar_file);
foreach ($entries as $entry) {
$entry->extract($outPath);
}
rar_close($rar_file);
}
这样就搞定用户上传的压缩包解压的问题了。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有