Extract ZIP File Using PHP Code - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Extract ZIP File Using PHP Code - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Tuesday, May 22, 2018

Extract ZIP File Using PHP Code

Extract ZIP File Using PHP Code

 

Zip Archieve is Very Usefull for Lots of file Single Archieve File.

But Let's Today We Extract That Zip in Our Server Using PHP Codes.


Let's Start.

In This Example I Upload A Zip File from HTML form And Extract Using PHP.

Our Form Code : (upload.html) 




<html>
    <head>
        <title>Extract Zip File.</title>
    </head>
    <body>
        <!---As You Enctype Is Necessary When We Uploading Files--->
        <form action="upload.php" method="post" enctype="multipart/form-data">
            <p>Please Select Your Zip file.</p>
            <input type="file" name="zip">
            <button type="submit">Upload</button>
            
        </form>
    </body>
</html>

(upload.php) codes 

<?php

//------Function For Extracting Zip File-------------
function extracted($file){
    
    //-----PHP ZIP Archive Object------
    $zipArchive    = new ZipArchive();
    $arch_filename = $file;
    
    //-----Opening Zip File-------------
    $result        = $zipArchive->open($file);
    
    //------If Open Successfully Then Extract it-------
    
    if ($result === TRUE) {
        
        //-----Directory Where Extracting File-------
         $zipArchive ->extractTo("extracted");
         return true;
    }
    else{
        return false;
    }
   
}
//---------End Extract Function--------------


//------Function For Extracting Zip Specific File Type-------------
function extracted_spec($file,$type){
    
    //-----PHP ZIP Archive Object------
    $zipArchive    = new ZipArchive();
    $arch_filename = $file;
    $dest_dir="extracted";
    //-----Opening Zip File-------------
    $result        = $zipArchive->open($file);
    
    //------If Open Successfully Then Extract it-------
    
    if ($result === TRUE) {
        
        //======================Finding File Type===============================================
        for ($i = 0; $i < $zipArchive->numFiles; ++$i) {
            $path = $zipArchive->getNameIndex($i);
            $ext  = pathinfo($path, PATHINFO_EXTENSION);
            if (!preg_match('/(?:'.$type.')/i', $ext))
                continue;
            $dest_basename = pathinfo($path, PATHINFO_BASENAME);
            copy("zip://{$arch_filename}#{$path}", "$dest_dir/{$dest_basename}");
        }
        
        
        //======================Extracting Specific File Type==================================
        $i = 0;
        for ($i = 0; $i < $zipArchive->numFiles; $i++) {
            $stat = $zipArchive->statIndex($i);
            $e    = pathinfo(basename($stat['name']), PATHINFO_EXTENSION);
            if ($e == $type) {
                $i = 1;
                return true;
            } 
        }
        if($i==0){
                return false;
        }
    }
    else{
        return false;
    }
   
}
//---------End Extract Function--------------

//---Set Directory For Zip Files---
$dir = "zips/";

//----Creating Unique File Name---------
$file = $dir.time().basename($_FILES["zip"]["name"]);

//---validation checking form submit or not----
if(isset($_POST["submit"])) {

    //----Move Uploaded File is a PHP Function for Moving Temp Location File To Specific Directory------
    if (move_uploaded_file($_FILES["zip"]["tmp_name"], $file)) {

        echo "File ". basename( $_FILES["zip"]["name"]). " has been uploaded.";
        //-----After Uploading Passing The File Path To Another Which Only Extract Our File.---
        
        if(extracted($file)){
            echo "File Successfully Extracted";
        }
        else{
            echo "Invalid Zip File Or Failed To Extract File";
        }

    } else {
        echo "Error During Uploading File";
    }
}
else{
    echo "No Data";
}

In Above Example I create Two Functions


1.For Extract All Contents inside File.

extracted("path/to/file"); 

2.For Specific File Type.

extracted_spec("path/to/file","file type e.g pdf");

No comments:

Post a Comment

Post Top Ad