Get cool snippets on php, css, jQuery, Magento and wordpress

Download files from multiple URL s and download it as zip format

Hello friends, here is the code for how to download files from multiple url and download is as zip format in PHP.
If you have any doubt in PHP function’s please refer php.net.

Let’s start!!!

We can give url’s as array format like below

$urls = array(

Create a random folder using mkdir() to download your file in it.


Create a zip file to add those file in it.

$zip = new ZipArchive;
$zip-&gt;open('file.zip', ZipArchive::CREATE);

File mentioned in the URL will be downloaded into our random folder which is created in our server.

foreach ($urls as $url){
$path = $rand.'/'.$path['basename'];
$fileopen = fopen($path, 'w');
$init = curl_init($url);
curl_setopt($init, CURLOPT_FILE, $fileopen);
$data = curl_exec($init);

Close the zip.

<?php $zip-&gt;close(); ?>

Check whether the zip file is created or not. If it is created, read that zip file and move it into an temporary variable and then delete the zip file using ‘unlink()‘ function.

$temp = file_get_contents("file.zip");

Here is our zip file download block:) We can enable download by using header function.

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=file.zip');
header('Content-Transfer-Encoding: binary');
echo $temp;

Function to delete our temporary folder.

function delete_directory($dirname) {
if (is_dir($dirname))
$dir_handle = opendir($dirname);
if (!$dir_handle)
return false;
while($file = readdir($dir_handle))
if ($file != "." &amp;&amp; $file != "..")
if (!is_dir($dirname."/".$file))
return true;

karthi s

2 thoughts on “Download files from multiple URL s and download it as zip format

  1. I have follow this tutorial and it works

    however, When I create a zip file with 6 music file whose size is more than 40MB

    I got Error

    This webpage is not found

    No webpage was found for the web address: http://localhost/sala/applications/getMusicPhoneAlbum/

    Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.

    When I removed three file than zip file will be 29 MB and the zip file can be downloaded.

    Any idea?? Thank

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: