How can i download zip file from url in php?

This class can download and extract a zip file from a remote site.

It can take a given URL of a remote site and download a ZIP file from there.

The class can also extract a ZIP file to a given local directory.


Details

{startverticalbanner}

The Urluploadunzip class is a free software distributed under the open source GNU licence.You can use it and modify it for your own or commercial use but please dont remove our credits.Despite its name,it does not deal with zip files alone.For example you can use it to upload an image by calling the url uploader funtion alone or maybe if you want to unzip an existing achieve you can just call the unzip file alone. For basic usage,you can view the example given.


  Classes of Trev Tune  >  Download ZIP from URL  > 
Download .zip .tar.gz
 > 
Support forum
 > 
Blog
 > 
Latest changes
 

Name:Base name:Description:Version:PHP version:Licenses:All time users:All time rank:Week users:Week rank:
Download ZIP from URL

Support forum

zip-downloader
Download and extract a zip file from a remote site
1.2.2
5.0
Freeware
GNU General Public License (GPL)
1112 users
3370
0 users
173
 

  Groups  
User ratings
  Applications  
Files
 

  Applications that use this package  

No pages of applications that use this class were specified.

If you know an application of this package, send a message to the author to add a link here.

 
Files
 

PHP provides ZipArchive Class which allows us to create a Zip file. This class makes file creation easier.

Programmatically Zip creation is mainly required when preparing the group of files and folders for downloading.

In the example, I am creating a function that will read all files and folders from the specified directory and add them to the ZipArchive class object.

Contents

  1. HTML
  2. PHP
  3. CSS
  4. Demo
  5. Conclusion

1. HTML

Creating a <form> which have two button elements, one for Create Zip and another for Download.

Completed Code

<div class='container'> <h2>Create and Download Zip file using PHP</h2> <form method='post' action=''> <input type='submit' name='create' value='Create Zip' />&nbsp; <input type='submit' name='download' value='Download' /> </form> </div>

I have created includes folder within the project where I stored some files and folders.

Create Zip

Create ZipArchive Class object for Zip file creation. Define a function createZip() to read files and directory from the specified directory path.

If file

If the reading value is the file then add it to zip object using addFile() method.

If directory

If the value is a directory then create an empty directory and call createZip() function where pass the directory path.

Download Zip

Check if the zip file exists or not. If it exists then download and remove it from the server.

Completed Code

<?php // Create ZIP file if(isset($_POST['create'])){ $zip = new ZipArchive(); $filename = "./myzipfile.zip"; if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) { exit("cannot open <$filename>\n"); } $dir = 'includes/'; // Create zip createZip($zip,$dir); $zip->close(); } // Create zip function createZip($zip,$dir){ if (is_dir($dir)){ if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false){ // If file if (is_file($dir.$file)) { if($file != '' && $file != '.' && $file != '..'){ $zip->addFile($dir.$file); } }else{ // If directory if(is_dir($dir.$file) ){ if($file != '' && $file != '.' && $file != '..'){ // Add empty directory $zip->addEmptyDir($dir.$file); $folder = $dir.$file.'/'; // Read data of the folder createZip($zip,$folder); } } } } closedir($dh); } } } // Download Created Zip file if(isset($_POST['download'])){ $filename = "myzipfile.zip"; if (file_exists($filename)) { header('Content-Type: application/zip'); header('Content-Disposition: attachment; filename="'.basename($filename).'"'); header('Content-Length: ' . filesize($filename)); flush(); readfile($filename); // delete file unlink($filename); } } ?>

3. CSS

.container{ margin: 0 auto; width: 50%; text-align: center; } input[type=submit]{ border: 0px; padding: 7px 15px; font-size: 16px; background-color: #00a1a1; color: white; font-weight: bold; }

4. Demo

View Demo

5. Conclusion

The above-defined function createZip() accepts the path of the directory as a parameter. It will read all files and folders from the path and add them to the zip file.

You can also use ZipArchive Class to unzip the Zip file.

If you found this tutorial helpful then don't forget to share.

How do I download a ZIP file from a link?

Google Chrome saves zip files as zip file by default. If not, you can right-click the zip-file link (if you have a right mouse button). It will say, "save link as".

How can I force download a zip file in PHP?

For force download, you have to set the header correctly. When you are setting the header for download the Zip file that time <coce>Content-type and Content-Disposition correctly. In Content-Disposition , add the attachment, it will suggest the browser to download the file instead of displaying it directly.

How do I download a file from a URL?

Download File from URL.
Go to the URL..
Right-click the webpage..
Select Save As....

How can I download URL from PHP?

Steps to download the file:.
Initialize a file URL to the variable..
Create cURL session..
Declare a variable and store the directory name where the downloaded file will save..
Use the basename() function to return the file basename if the file path is provided as a parameter..
Save the file to the given location..

Postingan terbaru

LIHAT SEMUA