项目涉及文档处理,用户上传的包括 zip 和 rar 压缩包,需要先将压缩包解压后再作处理。对于 zip 压缩包,由于 php 自带 zip 扩展,可以直接解压。
解压zip压缩包:
?
|
1
2
3
4
5
6
7
8
|
$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 扩展。
安装rar扩展:
?
|
1
2
3
4
5
6
7
8
9
10
11
|
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
|
配置rar扩展:
?
|
1
2
|
# 新建 /usr/local/php/conf.d/rar.ini,内容
extension=rar.so
|
重启 php-fpm ,看一下 phpinfo() ;
可以看到已经成功安装了 rar ,可以来测试一下解压 rar 文件。
解压RAR压缩包:
?
|
1
2
3
4
5
6
7
8
9
10
|
$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);
}
|
这样就搞定用户上传的压缩包解压的问题了。
总结
以上所述是小编给大家介绍的php解压缩zip和rar压缩包文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
原文链接:https://beltxman.com/archives/2531.html
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 59
-
2025-05-29 51
-
2025-05-29 95
-
2025-06-04 110
-
2025-05-25 91
热门评论

