PHP : Upload and Extract Zip File Contents

Sometimes we have to upload large number of files so we Compress all contents in a single ZIP file.Let me give you an Example: When you upload a zipped WordPress plugin file and it will Extract the Zip Contents in a folder.

In this article I will tell you how you can Upload and Extract (Unpack) Zip Files Contents with PHP.

Download

Upload and Extract Zip File

First of all We will Upload the Zip File. I am using Simple HTML and PHP code to upload and extract the Zip file.

HTML Code

Here is the Simple HTML Form Content to Upload the ZIP file.
Create index.html file and add this code in this file.

<form action="extract.php" method="post" enctype="multipart/form-data">
    Select Zip File to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Extract Zip" name="submit">
</form>

The output of the Above code will be look like:

HTML Zip Upload Form

Now let me Explain the Above Code.I have created an HTML form that contains a file upload input name fileToUpload and submitted to the file extract.php when user clicks on the Extract Zip (submit) button.

PHP Code to Upload and Extract the Zip File

Now we have to upload the zip file to our server, so I am using a simple PHP code that will upload the Zip file to your server and Extract its content.

Now create extract.php file and paste the below code.

<?php
 
if($_FILES["fileToUpload"]["name"]) {
        $file = $_FILES["fileToUpload"];
    $filename = $file["name"];
    $tmp_name = $file["tmp_name"];
    $type = $file["type"];
     
    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
 
    if(in_array($type,$accepted_types)) { //If it is Zipped/compressed File
        $okay = true;
    } 
     
    $continue = strtolower($name[1]) == 'zip' ? true : false; //Checking the file Extension
 
    if(!$continue) {
        $message = "The file you are trying to upload is not a .zip file. Please try again.";
    }
 
   
  /* here it is really happening */
        $ran = $name[0]."-".time()."-".rand(1,time());
        $targetdir = "zipped/".$ran;
        $targetzip = "zipped/".$ran.".zip";
 
    if(move_uploaded_file($tmp_name, $targetzip)) { //Uploading the Zip File
         
        /* Extracting Zip File */
 
        $zip = new ZipArchive();
        $x = $zip->open($targetzip);  // open the zip file to extract
        if ($x === true) {
            $zip->extractTo($targetdir); // place in the directory with same name  
            $zip->close();
     
            unlink($targetzip); //Deleting the Zipped file
        }
        $message = "Your <strong>{$ran}.zip</strong> file was uploaded and unpacked.";
 
    } else {    
        $message = "There was a problem with the upload. Please try again.";
    }
}
echo $message;
 
?>

I hope you like this script and please don’t forget to share it with your friends on social networks.

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

3 Responses

  1. Sajjad says:

    Exactly , This is the easiest way to extract zip file in PHP.
    $zip = new ZipArchive;
    $res = $zip->open($filename); // Your filename
    if ($res === TRUE) {
    $zip->extractTo(‘./’);
    $zip->close();
    }
    I am unzip files from server using this snippet. Thanks for sharing this.

  2. Alex Cheddar says:

    THANK YOU.

Leave a Reply

Your email address will not be published. Required fields are marked *