php ZipArchive实现多文件打包下载实例
php  /  管理员 发布于 7年前   197
实例代码: 注意:里面的路径全部用绝对路径,不然会找不到文件 附赠其他操作: 解压缩zip文件 获取解压文件目录 大家可以在本地测试下,感谢大家的学习和对AIDI的支持。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 );}
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); }
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号