Example of how PHP/ThinkPHP can package and download files in batches,

Source: Internet
Author: User
Tags ziparchive

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.