Example of how PHP/ThinkPHP can package and download files in batches,
Preface
This article describes how to package and download files in batches using PHP/ThinkPHP. We will share the content for your reference. If you don't talk about it, let's take a look at the details:
Requirement Description:
There are several files, including images and documents. You need to automatically package the package into a compressed package based on the conditions to download the package.
Solution (ZipArchive class ):
PHP provides the ZipArchive class to implement this function. demo:
<?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; // compressed file name $ filename = 'download.zip '; // create a zip package $ zip-> open ($ filename, \ ZipArchive: OVERWRITE ); // Add one image to the compressed foreach ($ images as $ key = >$ value) {$ zip-> addFile ($ value );} // zip $ zip-> close (); // you can directly redirect the download header ('location :'. $ filename); // or the output download header ("Cache-Control: public"); header ("Content-Description: File Transfer"); header ('content-disposition: attachment; filename = '. basename ($ filename); // file name header ("Content-Type: application/force-download"); header ("Content-Transfer-Encoding: binary "); header ('content-Length :'. filesize ($ filename); // tell the browser the file size readfile ($ filename );
The difference is that the path must be correct at the time of reference and ends.
References:
Http://www.php.net/manual/zh/class.ziparchive.php
Http://dengrongguan12.github.io/blog/2016/php-ziparchive/
Summary
Well, that's probably the case. The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.