如何用PHP ZipArchive实现多个长尾关键词文件打包下载?

2026-04-01 08:031阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计401个文字,预计阅读时间需要2分钟。

如何用PHP ZipArchive实现多个长尾关键词文件打包下载?

实例代码:

phppublic function downLoad($dataUrl, $saveName) { $datalist=['/public/introduce/110.docx', '/public/upfile/110.zip']; // 打印文件列表 print_r($datalist); die;

$filename=ROOT_PATH . '/public/' . $saveName . '.zip'; if (file_exists($filename)) { // 文件存在处理逻辑 } else { // 文件不存在处理逻辑 }}

实例代码:

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

注意:里面的路径全部用绝对路径,不然会找不到文件

如何用PHP ZipArchive实现多个长尾关键词文件打包下载?

附赠其他操作:

解压缩zip文件

public function unzip_file($file, $dir){ // 实例化对象 $zip = new ZipArchive() ; //打开zip文档,如果打开失败返回提示信息 if ($zip->open($file) !== TRUE) { die ("Could not open archive"); } //将压缩文件解压到指定的目录下 $zip->extractTo($dir); //关闭zip文档 $zip->close(); }

获取解压文件目录

public function loopFun($dir) { $handle = opendir($dir."."); //定义用于存储文件名的数组 $array_file = array(); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $array_file[] = $dir.'/'.$file; //输出文件名 } } closedir($handle); return $array_file; //print_r($array_file); }

大家可以在本地测试下,感谢大家的学习和对易盾网络的支持。

本文共计401个文字,预计阅读时间需要2分钟。

如何用PHP ZipArchive实现多个长尾关键词文件打包下载?

实例代码:

phppublic function downLoad($dataUrl, $saveName) { $datalist=['/public/introduce/110.docx', '/public/upfile/110.zip']; // 打印文件列表 print_r($datalist); die;

$filename=ROOT_PATH . '/public/' . $saveName . '.zip'; if (file_exists($filename)) { // 文件存在处理逻辑 } else { // 文件不存在处理逻辑 }}

实例代码:

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

注意:里面的路径全部用绝对路径,不然会找不到文件

如何用PHP ZipArchive实现多个长尾关键词文件打包下载?

附赠其他操作:

解压缩zip文件

public function unzip_file($file, $dir){ // 实例化对象 $zip = new ZipArchive() ; //打开zip文档,如果打开失败返回提示信息 if ($zip->open($file) !== TRUE) { die ("Could not open archive"); } //将压缩文件解压到指定的目录下 $zip->extractTo($dir); //关闭zip文档 $zip->close(); }

获取解压文件目录

public function loopFun($dir) { $handle = opendir($dir."."); //定义用于存储文件名的数组 $array_file = array(); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $array_file[] = $dir.'/'.$file; //输出文件名 } } closedir($handle); return $array_file; //print_r($array_file); }

大家可以在本地测试下,感谢大家的学习和对易盾网络的支持。