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
Create a random folder using mkdir() to download your file in it.
Create a zip file to add those file in it.
File mentioned in the URL will be downloaded into our random folder which is created in our server.
Close the zip.
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.
Here is our zip file download block:) We can enable download by using header function.
Function to delete our temporary folder.
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
<?php $urls = array( 'http://skidvis.com/WordpressCheatSheet.pdf', 'http://andywibbels.com/files/WordPress_Cheatsheet_v1.pdf' ); ?>
Create a random folder using mkdir() to download your file in it.
<?php $rand=rand(1,1000000000); $mkdir=mkdir($rand); ?>
Create a zip file to add those file in it.
<?php $zip = new ZipArchive; $zip->open('file.zip', ZipArchive::CREATE); ?>
File mentioned in the URL will be downloaded into our random folder which is created in our server.
<?php foreach ($urls as $url){ $path=pathinfo($url); $path = $rand.'/'.$path['basename']; $zip->addFile($path); $fileopen = fopen($path, 'w'); $init = curl_init($url); curl_setopt($init, CURLOPT_FILE, $fileopen); $data = curl_exec($init); curl_close($init); fclose($fileopen); } ?>
Close the zip.
<?php $zip->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.
<?php if(file_exists("file.zip")){ $temp = file_get_contents("file.zip"); unlink("file.zip"); } delete_directory($rand); ?>
Here is our zip file download block:) We can enable download by using header function.
<?php 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.
<?php function delete_directory($dirname) { if (is_dir($dirname)) $dir_handle = opendir($dirname); if (!$dir_handle) return false; while($file = readdir($dir_handle)) { if ($file != "." && $file != "..") { if (!is_dir($dirname."/".$file)) unlink($dirname."/".$file); else delete_directory($dirname.'/'.$file); } } closedir($dir_handle); rmdir($dirname); return true; } ?>
Great Article Thank !
ReplyDeleteI have follow this tutorial and it works
ReplyDeletehowever, 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