24 November 2009

PHP: How to upload ZIP, check if file exists within it & extract to the server

I had to do this today for a job. It took far longer than it should have. The resources online are minimal and php.net does not mention any keywords like file_exists on manual for the function needed to do this. so...

Here is the best way to check if an uploaded zip archive contains a certain file and if so, extract the entire contents to a directory on your server:


Please see right hand panel before copying>

$zipFile=$_FILES["your file field name here"];
$checkFile="doesThisFileExist.html";
$exractPath="directory/to/extract/zip";

if($zipFile["error"] == UPLOAD_ERR_OK){
    if(strchr($zipFile['tmp_name'],'.zip')){
        $zip = new ZipArchive;
        $res = $zip->open($zipFile["tmp_name"]);
        if ($res === TRUE) {
            if(is_numeric($zip->locateName($checkFile))){
                $zip->extractTo($exractPath);
                //put extra functions here if you want
                $zip->close();
            }else{
                echo "ERROR: Uploaded zip archive is missing a required file!";
            }
        }else{
            echo "ERROR: Could not open uloaded zip archive";
        }
    }else{
        echo "ERROR: Uploaded file is not a zip archive"
    }
}else{
    echo  "ERROR: The upload was not successful";
}

3 comments:

Anonymous said...

No you did not help me!

Unknown said...

coach outlet
michael kors handbags
longchamp bags
adidas nmd
michael kors outlet online
polo ralph lauren outlet online
cheap mlb jerseys
coach factory outlet
cheap uggs
cheap uggs
20161228caiyan

asdasd said...

pg เว็บสล็อต สมัคร ฟรี ฟรี สมัคร PG SLOT กับเว็บไซต์ของเรา รับโปรโมชั่นดีๆที่หาไม่ได้จากที่อื่น พร้อมทั้งระบบการสมัครสมาชิกที่รองรับให้ท่านสามารถสมัครได้อย่างรวดเร็ว ผ่านทางระบบออโต้

Post a Comment

Cron Design Studio: Dublin based web design & software development