批量打包下载文件(php版)

颓废 2019年5月19日10:17:03评论483 views字数 1164阅读3分52秒阅读模式
摘要

PHP提供了ZipArchive 类可实现这功能 <?php $files = array('image.jpeg','text.txt','music.wav'); $zipname = 'enter_any_name_for_the_zipped_file.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); ///Then download the zipped file. header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipname); header('Content-Length: ' . filesize($zipname)); readfile($zipname); ?> ThinkPHP版

PHP提供了ZipArchive 类可实现这功能

<?php   $files = array('image.jpeg','text.txt','music.wav'); $zipname = 'enter_any_name_for_the_zipped_file.zip'; $zip = new ZipArchive; $zip->open($zipname, ZipArchive::CREATE); foreach ($files as $file) {   $zip->addFile($file); } $zip->close();   ///Then download the zipped file. header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipname); header('Content-Length: ' . filesize($zipname)); readfile($zipname);   ?>

ThinkPHP版

$zip = new /ZipArchive; //压缩文件名 $filename = 'download.zip'; //新建zip压缩包 $zip->open($filename,/ZipArchive::OVERWRITE); //把图片一张一张加进去压缩 foreach ($images as $key => $value) {     $zip->addFile($value); } //打包zip $zip->close();   //可以直接重定向下载 header('Location:'.$filename);   //或者输出下载 header("Cache-Control: public");  header("Content-Description: File Transfer");  header('Content-disposition: attachment; filename='.basename($filename)); //文件名    header("Content-Type: application/force-download"); header("Content-Transfer-Encoding: binary"); header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小    readfile($filename);

主要是有多个文件,包含图片,文档。需要根据条件自动打包成压缩包,提供下载。

附上两条参考

http://www.php.net/manual/zh/class.ziparchive.php
http://dengrongguan12.github.io/blog/2016/php-ziparchive/

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
颓废
  • 本文由 发表于 2019年5月19日10:17:03
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   批量打包下载文件(php版)https://cn-sec.com/archives/68457.html

发表评论

匿名网友 填写信息