Tuesday, 18 September 2012

Extracting Zip in purephp

When we are doing dynamic sites, their araise situation where we want to extract zipped files. so iam explaining here one way to extract zip files using corephp.
 for doing this first you have to ensure that your server has zip extension enabled. then if it is enabled simply follow the steps which iam explaining:-

1) First you have to include a zip library. here iam using pclzip library.
just download file from http://www.phpconcept.net/pclzip/pclzip-downloads



2)Now you have to include it in your zip uploading page like :-
include("pclzip.lib.php");

3) Uploading html code is similar to the image uploading itself
 <form action="theme-create.php" method="post" enctype="multipart/form-data">
 <div class="row">
                            <div class="row150">
                                <h6>Upload zip:
                                <span class="req">*</span></h6>
                            </div>
                            <div class="row350">
                                <input type="file" name="txtFile1"  size="40" maxlength="90" tabindex="3" class="browse"/>
                            
                            </div>
                        </div>
</form>

4) create a class named Uploader and copy below code to it 
<?php
/*
 * Date:8-16-2012
 * Login Form , entry to the application
 * Auhthor : Litto chacko
 * Email:littochackomp@gmail.com
*/
    class Uploader
    {
        private $destinationPath;
        private $errorMessage;
        private $extensions;
        private $allowAll;
        private $maxSize;
        private $uploadName;
private $seqnence;
        
        function __construct($path){
            $this->destinationPath  =   $path;
            $this->allowAll =   false;
        }
        
        function allowAllFormats(){
            $this->allowAll =   true;
        }
        
        function setMaxSize($sizeMB){
            $this->maxSize  =   $sizeMB * (1024*1024);
        }
        
        function setExtensions($options){
            $this->extensions   =   $options;
        }
        
function setSameFileName(){
$this->sameFileName = true;
$this->sameName = true;
}
        function getExtension($string){
            $ext = "";
            try{
                    $parts = explode(".",$string);
                    $ext = strtolower($parts[count($parts)-1]);
            }catch(Exception $c){
                    $ext = "";
            }
            return $ext;
}
        
        function setMessage($message){
            $this->errorMessage =   $message;
        }
        
        function getMessage(){
            return $this->errorMessage;
        }
        
        function getUploadName(){
            return $this->uploadName;
        }
        function setSequence($seq){
$this->imageSeq = $seq;
}
function sameName($true){
$this->sameName = $true;
}
        function uploadFile($fileBrowse){
            $result =   false;
            $size   =   $_FILES[$fileBrowse]["size"];
            $name   =   $_FILES[$fileBrowse]["name"];
            $ext    =   $this->getExtension($name);
            if(!is_dir($this->destinationPath)){
                $this->setMessage("Destination folder is not a directory ");
            }else if(!is_writable($this->destinationPath)){
                $this->setMessage("Destination is not writable !");
            }else if(empty($name)){
                $this->setMessage("File not selected ");
            }else if($size>$this->maxSize){
                $this->setMessage("Too large file !");
            }else if($this->allowAll || (!$this->allowAll && in_array($ext,$this->extensions))){
                $util   =   new Utilities();
if($this->sameName==false){
                $this->uploadName   =  $this->imageSeq."-".substr(md5(rand(1111,9999)),0,8).$util->getRandom().rand(1111,1000).rand(99,9999).".".$ext;
                }else{
$this->uploadName= $name;
}
                if(move_uploaded_file($_FILES[$fileBrowse]["tmp_name"],$this->destinationPath.$this->uploadName)){
                    $result =   true;
                }else{
                    $this->setMessage("Upload failed , try later !");
                }
            }else{
                $this->setMessage("Invalid file format !");
            }
            return $result;
        }
        
        function deleteUploaded(){
            unlink($this->destinationPath.$this->uploadName);
        }
        
    }

?>


5) So in the action side you have to do code like this... create afolder named themes in the same folder.. so that extracted files will be extracted to this folder

          $uploader   =   new Uploader($absDirName.'/');//object of uploader
            $uploader->setExtensions(array('zip'));
            $uploader->setSequence('theme');
            $uploader->setMaxSize(10);
            if($uploader->uploadFile("txtFile1")){
        
                 $image1     =   $uploader->getUploadName(); 
                
            } 
    

$file_to_open=$absDirName.'/'.$image1;
$zip = new PclZip($file_to_open);// creating object of pclzip
$target="/themes/";
$unique_folder="theme";
chmod($target.'/'.$txtTitle,0777);//changing permission of folder to writable
if ($zip->extract(PCLZIP_OPT_PATH, $target) == 0) {//extracting zip files
   $message    =   new Message('problem in extracting files','error');
            $message->setMessage();
              header('Location:themes.php');
            exit;
6) Now zip file will be extracted

No comments:

Post a Comment