Wednesday 5 March 2014

Unzipping Files in Core PHP

In this Post Iam explaining about how to handle zip files in php. In many projects their araise situation where you want to upload zip file for eg: For a blogger like website you want to add large number of themes dynamically. Without this unzip function it will be practically difficult to upload folder containing themes.
In Php there is a library for handling this zip files. The name of  that Library is ZipArchive.
In Latest versions of PHP you didn't want to install anything for it,it comes default with PHP package.
So I will explain step by step How to acheive this:-

1) We want to have an html form for uploading zip files.so my html code goes here:-
<html>
<body>
<form class="form-horizontal" name="addproduct" method="post" enctype="multipart/form-data" action="addtemplate.php">
<div class="control-group">
                   
                                <div class="span5">    <label class="control-label" for="form-field-1">Template(Zip File)</label>

                                    <div class="controls">

                                        <input type="file" id="id-input-file-2" name="txtzipFile"/>
                                           
                                    </div></div>
                                </div>

        <div class="form-actions">
                                    <button class="btn btn-info" type="submit" name="submit">
                                        <i class="icon-ok bigger-110"></i>
                                        Submit
                                    </button></div>


</body>
</html>


Above is a form to upload a file. Its action is set to addtemplate.php. & File name is txtZipFile


2) In the action, we are going to do this :- upload the zip file to a location then From that location we will unzip our file and store it in another Location.
So here we go:-

<?php

if(isset($_POST['submit'])){


$name="somename";//demo name to be given to unzip folder

 $absDirNamezip =   dirname(dirname(__FILE__)).'/user/template'; //setting path to upload file

    $rnd=rand(100,999);
     $rnd=$rnd."_";
    $fileName = $rnd.trim($_FILES['files']['name']);//setting file name

$tmpname=$_FILES['files']['tmp_name'][$i];
$target=  $absDirNamezip.'/'.$fileName;
move_uploaded_file($tmpname,$target);//uploading file
$templateloc =$fileName;
            mkdir($absDirNamezip.'/'.$name);//making directory of demo name
            chmod($absDirNamezip.'/'.$name,0777);//giving permissions

        $file = $absDirNamezip.'/'.$templateloc;
  $fileloc=$absDirNamezip.'/'.$name;

// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);

$zip = new ZipArchive;
$res = $zip->open($file);//opening zip file
if ($res === TRUE) {
  $zip->extractTo($fileloc);//extracting zip file
  $zip->close();
  echo 'Success';

} else {
  echo 'Failed!';

}
unlink($file);//deleting the zipped file uploaded 
}

?>
Now you have unzipped file in the folder you created.Just check it :-)
Have a Great day :-)





No comments:

Post a Comment