Multiple zip file creation and download

Multiple zip file creation and download

multiple zip file creation and download

Provides static methods for creating, extracting, and opening zip archives. This example shows how to create and extract a zip archive by using the ZipFile. Download software free to easily zip, unZip and compress files. You can use the ZipArchive class to create a ZIP file and stream it to the client. Something like: $files = array('www.cronistalascolonias.com.ar', 'www.cronistalascolonias.com.ar', 'www.cronistalascolonias.com.ar');. multiple zip file creation and download

PHP - How to create zip file and download multiple files using ZipArchive ?

In this PHP Tutorial, I am going to tell you how to create zip file using PHP's ZIP class "ZipArchive".

You can use this example to download multiple files at the same time by creating zip file, you can simply add the downloading files in a zip with the help of PHP's ZIP class.

In this example, I will use method to open or create any zip file and method is used to add files to archive from the given path and method is used to close zip file safely.

Example:
<?php/* create a compressed zip file */functioncreateZipArchive($files=array(), $destination='', $overwrite=false) { if(file_exists($destination) &&!$overwrite) { returnfalse; } $validFiles=array(); if(is_array($files)) { foreach($filesas$file) { if(file_exists($file)) { $validFiles[] =$file; } } } if(count($validFiles)) { $zip=new ZipArchive(); if($zip->open($destination,$overwrite? ZIPARCHIVE::OVERWRITE: ZIPARCHIVE::CREATE) ==true) { foreach($validFilesas$file) { $zip->addFile($file,$file); } $zip->close(); returnfile_exists($destination); }else{ returnfalse; } }else{ returnfalse; } } $fileName='www.cronistalascolonias.com.ar'; $files=array('uploads/www.cronistalascolonias.com.ar', 'uploads/www.cronistalascolonias.com.ar'); $result= createZipArchive($files, $fileName); header("Content-Disposition: attachment; filename=\"".$fileName."\""); header("Content-Length: ".filesize($fileName)); readfile($fileName); ?>

You have to set header correctly to force download. You can pass the array of files in function.

Источник: www.cronistalascolonias.com.ar

Multiple zip file creation and download

3 thoughts to “Multiple zip file creation and download”

Leave a Reply

Your email address will not be published. Required fields are marked *