Home > Backend Development > PHP Tutorial > How to package and download multiple files in PHP

How to package and download multiple files in PHP

墨辰丷
Release: 2023-03-26 21:24:01
Original
1762 people have browsed it

This article mainly introduces the method of packaging and downloading multiple files in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

When multiple files need to be downloaded at the same time, most browsers do not support simultaneous downloading of multiple files. JavaScript scripts can be used to dynamically generate multiple links, but this requires the user to click the download dialog box multiple times, resulting in a poor user experience. , and some browsers are not yet compatible. At this time, multiple file package downloads can help you solve this problem.

$filename = "test.zip";
$datalist=array('./pubfile/1.jpg','./pubfile/2.jpg');
if(!file_exists($filename)){
 $zip = new ZipArchive();
 if ($zip->open($filename, ZipArchive::CREATE)==TRUE) {
  foreach( $datalist as $val){
   if(file_exists($val)){
    $zip->addFile( $val, basename($val));
   }
  }
  $zip->close();
 }
}
if(!file_exists($filename)){
 exit("无法找到文件");
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小
@readfile($filename);
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP file upload function——Multiple filesUpload

phpMultiple filesCompression function

AjaxuploadMultiple filesUpload

The above is the detailed content of How to package and download multiple files in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template