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


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

<body> 

<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" />

</form>


</body>

create-zip-file.php
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.
Steps:
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.


<?php
 
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.
   $zip->close();
    }
       
  }
  // push to download the zip
  header('Content-type: application/zip');
  header('Content-Disposition: attachment; filename="skptricks.zip"');
  readfile($zip_name);
  // remove zip file is exists in temp path
  unlink($zip_name);
  //remove image directory once zip file created
  removedir($folderLocation); 
 }
 
} 
 // 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") 
           rrmdir($dir."/".$object); 
        else unlink   ($dir."/".$object);
      }
    }
    reset($objects);
    rmdir($dir);
  }
 } 
 
?>

Video Link :
https://youtu.be/KutrZbXgdHM

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: