Unzip an uploaded file using PHP

Categories: PHP

0 comments

Posted on September 15, 2010


I have been looking everywhere to try and find out how to do this. Some ‘solutions’ on the web left me at a dead end, but I finally worked out how to do it.

This is presuming you don’t have shell access to your server.

Simply create a file in the same directory as the ZIP file you wish to extract, and call it whatever you like. I use ‘unzip.php’.

Edit the file, and paste in the following code.

 <?php
     $zip = new ZipArchive;
     $res = $zip->open(’my_zip_file.zip’);
     if ($res === TRUE) {
         $zip->extractTo(’my_extract_to_dir/’);
         $zip->close();
         echo ‘ok’;
     } else {
         echo ‘failed’;
     }
?>

Change the name of ‘my_zip_file.zip’ to whatever your ZIP file is called. Then change the ‘my_extract_to_dir/’ to whatever directory on your server you wish to extract the ZIP to.

Once you have done, save the file and go to it through your browser ( http://www.youdomain.com/unzip.php ). If your browser echo’s out ‘ok’ then your file is extraced. If your browser echo’s out ‘failed’ you’ve filled in one of the bits of info wrong.

Basically it extracts the ZIP file into the directory you specify… make sure the directory you want to extract it to has write permissions.

Comments

No Responses to “Unzip an uploaded file using PHP”

Leave a comment




By submitting a comment here you grant domioanna a perpetual license to reproduce your words and name/web site in attribution. Inappropriate comments will be removed at admin's discretion.