項目涉及文檔處理,用戶上傳的包括 zip 和 rar 壓縮包,需要先將壓縮包解壓后再作處理。對于 zip 壓縮包,由于 php 自帶 zip 擴展,可以直接解壓。
解壓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 擴展。
安裝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
配置rar擴展:
# 新建 /usr/local/php/conf.d/rar.ini,內(nèi)容
extension=rar.so
重啟 php-fpm
,看一下 phpinfo() ;
可以看到已經(jīng)成功安裝了 rar ,可以來測試一下解壓 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);
}
這樣就搞定用戶上傳的壓縮包解壓的問題了。
總結(jié)
以上所述是小編給大家介紹的php解壓縮zip和rar壓縮包文件的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
您可能感興趣的文章:- php安裝php_rar擴展實現(xiàn)rar文件讀取和解壓的方法
- php 解壓rar文件及zip文件的方法
- PHP執(zhí)行zip與rar解壓縮方法實現(xiàn)代碼
- ThinkPHP5.0框架驗證碼功能實現(xiàn)方法【基于第三方擴展包】
- PHP實現(xiàn)rar解壓讀取擴展包小結(jié)