pepebe
4/15/2014 - 3:16 PM

Unzip a package on your server from one directory into another one.

Unzip a package on your server from one directory into another one.

<?php
$filename = "";

if(empty($filename)){

    $filename = basename($filename,'.zip');
    
    $tmp_path    = 'assets/tmp/';
    $target_path = 'assets/tpl/';
    $basepath    = '/paas/cXXXX/www/'; /* Add your server path here... */
    
    $tmp_path    = $basepath.$tmp_path.$filename.".zip";
    $target_path = $basepath.$target_path.$filename."/";
    
    function extractZip($tmp_path, $target_path, $filename)
    {
        if (file_exists($tmp_path)){
            $zip = new ZipArchive;
            $res = $zip->open($tmp_path);
            if ($res === TRUE)
            {
                $zip->extractTo($target_path);
                $zip->close();
                return '<p>' . $filename . ' file has been extracted to: ' . $target_path . '</p>';
            }
            else
            {
                return '<p>There was a problem opening the zip file: ' . $res . '</p>';
            }
        }
        else{
            return "<p>There was an error downloading, writing or accessing the zip file.</p>";
        }
    }
    
    
    echo extractZip($tmp_path,$target_path,$filename);

}