Monday 3 December 2012

Unzipping a zipped file using PHP


Below is a code to unzip a zipped file. Hope you can make the best of it.

function.php

<?php
    function unzip($ziplocation,$unzippedLocation){
        if(exec("unzip $ziplocation",$arr)){
            mkdir($newLocation);
            for($i = 1;$i< count($arr);$i++){
                $file = trim(preg_replace("~inflating: ~","",$arr[$i]));
                copy($ziplocation.'/'.$file,$unzippedLocation.'/'.$file);
                unlink($ziplocation.'/'.$file);
            }
            return TRUE;
        }else{
            return FALSE;
        }
    }
?>

index.php


<?php
include 'functions.php';
if(unzip('zipfolder/test.zip','unziped/newZip'))
    echo 'Success!';
else
    echo 'Error';
?>

Thank You!

No comments:

Post a Comment