Saturday, April 14, 2018

Upload and Create a Zip Files In PHP

This tutorial explains how to upload multiple file and zip the file using php. Here we are creating a zip file using ZipArchive class of php. Recently we have published a post related to ZipArchive class, where we have given complete explanation about ZipArchive class and with the help of this class you can create a zip file and extract a zip file. Lets check out the below example for the further information.
1.  Create A Zip File Using PHP
2.  Unzip Or Extract Zip File Using PHP

Create Zip File After Upload Using PHP

Here we have tried our best to make this example as simple as possible. Basically we are uploading the multiple image, pdf, doc files using the upload operation and once the files are upload to server then our script help us to archive all files into a single zip file. Once the zip file is created then, it will download the zip file to local machine automatically.

Lets see the complete example for Upload and Zip files in php.

Upload and create zip file

This consists of HTML and PHP script. With the help of html form we are uploading the multiple files.
// including the upload the zip php script 
include 'create-zip-file.php';
<html >
<link rel="stylesheet" type="text/css" href="design.css">


<h3>Upload and create zip file</h3>
<form method="post" action="" name="login" enctype="multipart/form-data">
Choose a zip file to upload: <input type="file" name="uploadzip[]" multiple="" />
<input type="submit" name="submit" value="Upload" />



This script help us to create a zip file  and once zip file is created, then it will automatically download the zip file to local machine.
1. When user send a upload request for the files, then this script will store all the files inside the "Images" Folder.
2. With the help of ZipArchive class, we are retrieving all the images from the images folder and creating a zip file.
3. Once zip file is created, then this script automatically download the zip file to local machine. 
4. Final step where we are deleting a the zip file and all image inside the "Images" folder from the sever location.

if( isset($_POST['submit']) )  {
 $filename = $_FILES['uploadzip']['name'];
 $source = $_FILES["uploadzip"]["tmp_name"];
 $type = $_FILES["uploadzip"]["type"]; 
 //echo sizeof($filename) ;
 //check file is selected for upload
 if(isset($_FILES['uploadzip']['name']) != ""){
      //First check whether zip extension is enabled or not
  if(extension_loaded('zip')) {
   //create the directory named as "images"
   $folderLocation = "images" ; 
   if (!file_exists($folderLocation)) {
    mkdir($folderLocation, 0777, true);
   $zip_name = time().".zip"; // Zip file name 
   $zip = new ZipArchive;
   if ($zip->open($zip_name, ZipArchive::CREATE) == TRUE){          
    foreach($_FILES["uploadzip"]["tmp_name"] as $key=>$tmp_name){
     $temp = $_FILES["uploadzip"]["tmp_name"][$key];
     $actualfile = $_FILES["uploadzip"]["name"][$key];
     // moving image files to temporary locati0n that is "images/"
     move_uploaded_file($temp, $folderLocation."/".$actualfile);
     // adding image file to zip
     $zip->addFile($folderLocation."/".$actualfile, $actualfile );
   // All files are added, so close the zip file.
  // push to download the zip
  header('Content-type: application/zip');
  header('Content-Disposition: attachment; filename=""');
  // remove zip file is exists in temp path
  //remove image directory once zip file created
 // user defined function to remove directory with their content
function removedir($dir) {
  if (is_dir($dir)) {
    $objects = scandir($dir);
    foreach ($objects as $object) {
      if ($object != "." && $object != "..") {
        if (filetype($dir."/".$object) == "dir") 
        else unlink   ($dir."/".$object);

Video Link :

Download  Link :

This is all about uploading the multiple files and creating a zip file. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.

1 comment: