Home > Article > Backend Development > PHP multi-file package download implementation code
I recently sorted out the documents and found an example code for php multi-file packaging and downloading. I sorted it out and streamlined it a little for sharing. This article mainly introduces to you the example code of PHP multi-file packaging and downloading. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help 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 the user needs to click the download dialog box multiple times. The experience is not good, and some browsers are not compatible yet. 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);
Related recommendations:
Examples of two methods of php multi-file packaging and downloading
How to implement php multi-file upload encapsulation
How to implement php multi-file upload
The above is the detailed content of PHP multi-file package download implementation code. For more information, please follow other related articles on the PHP Chinese website!