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('無法打開文件,或者文件創(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("無法找到文件"); //即使創(chuàng)建,仍有可能失敗
}
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.basename($filename));
header('Content-Length: ' . filesize($filename));
@readfile($filename);
@unlink ( $filename );
}
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();
}