php ZipArchive實(shí)現(xiàn)多文件打包下載實(shí)例
更新時(shí)間:2019年10月31日 15:44:27 作者:赤驥
在本篇文章里我們給各位整理了關(guān)于php ZipArchive實(shí)現(xiàn)多文件打包下載實(shí)例以及相關(guān)代碼,需要的朋友們可以學(xué)習(xí)下。
實(shí)例代碼:
public function downLoad($dataUrl,$saveName)
{
$datalist = [
ROOT_PATH.'/public/introduce/110.docx',
ROOT_PATH.'/public/upfile/110.zip'
];
// print_r($datalist);die;
$filename = ROOT_PATH.'\public\/'.$saveName.'.zip';
if(file_exists($filename)){
unlink($filename);
}
$zip = new \ZipArchive();
if ($zip->open($filename,\ZipArchive::CREATE)!== true){
exit('無(wú)法打開文件,或者文件創(chuàng)建失敗');
}
foreach ($dataUrl as $index => $item) {
if (DIRECTORY_SEPARATOR=='\\'){
$item = str_replace('/',DIRECTORY_SEPARATOR,$item);
$filename = str_replace('/',DIRECTORY_SEPARATOR,$filename);
}
// var_dump($item);
// var_dump(file_exists($item));die;
if (file_exists($item)){
$zip->addFile($item,basename($item));
}
}
$zip->close();
if(!file_exists($filename)){
exit("無(wú)法找到文件"); //即使創(chuàng)建,仍有可能失敗
}
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.basename($filename));
header('Content-Length: ' . filesize($filename));
@readfile($filename);
@unlink ( $filename );
}
注意:里面的路徑全部用絕對(duì)路徑,不然會(huì)找不到文件
附贈(zèng)其他操作:
解壓縮zip文件
public function unzip_file($file, $dir){
// 實(shí)例化對(duì)象
$zip = new ZipArchive() ;
//打開zip文檔,如果打開失敗返回提示信息
if ($zip->open($file) !== TRUE) {
die ("Could not open archive");
}
//將壓縮文件解壓到指定的目錄下
$zip->extractTo($dir);
//關(guān)閉zip文檔
$zip->close();
}
獲取解壓文件目錄
public function loopFun($dir)
{
$handle = opendir($dir.".");
//定義用于存儲(chǔ)文件名的數(shù)組
$array_file = array();
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..") {
$array_file[] = $dir.'/'.$file; //輸出文件名
}
}
closedir($handle);
return $array_file;
//print_r($array_file);
}
大家可以在本地測(cè)試下,感謝大家的學(xué)習(xí)和對(duì)腳本之家的支持。
您可能感興趣的文章:
- php使用ZipArchive函數(shù)實(shí)現(xiàn)文件的壓縮與解壓縮
- php利用ZipArchive類操作文件的實(shí)例
- php打包壓縮文件之ZipArchive方法用法分析
- php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解決方法
- php的ZipArchive類用法實(shí)例
- php ZipArchive壓縮函數(shù)詳解實(shí)例
- php文件打包 下載之使用PHP自帶的ZipArchive壓縮文件并下載打包好的文件
- php?ZipArchive解壓縮實(shí)現(xiàn)后臺(tái)管理升級(jí)問(wèn)題詳解
相關(guān)文章
PHP實(shí)現(xiàn)通過(guò)二維數(shù)組鍵值獲取一維鍵名操作示例
這篇文章主要介紹了PHP實(shí)現(xiàn)通過(guò)二維數(shù)組鍵值獲取一維鍵名操作,涉及php數(shù)組遍歷、判斷、搜索等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
php使用pdo連接mssql server數(shù)據(jù)庫(kù)實(shí)例
這篇文章主要介紹了php使用pdo連接mssql server數(shù)據(jù)庫(kù)的方法,以實(shí)例形式分析了php使用pdo連接mssql server數(shù)據(jù)庫(kù)的技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2014-12-12

