PHP - Unzip an uploaded file using php

If you dont have shell access to your server and need to unzip a file on your php server you can use the script below:

 


<?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';
     }
?> 


Basically it extracts the zip file into the directory you specify… make sure the directory you want to extract it to has write permissions. More info on this script is available on the php website

One issue I ran into with this script was that I could not modify the files via my ftp program as they did not have permission set for this. In my next entry I will show you the chmod script I used to chmod this directory to the needed permissions.

 

Comments   

 
Thomas
#21 Thomas 2013-02-01 07:07
Danke, das Script funktioniert prima!
Quote
 
 
Barcodeq
#20 Barcodeq 2013-01-24 17:03
The most helpfull code ever, thank you!!!!
Quote
 
 
Andy
#19 Andy 2013-01-18 13:05
Amazing. How can a few lines of code do something so complicated? I read the comments and was cynical, put the code into my script and sat back and waited for the inevitable error message. It didn't come! Thank you.
Quote
 
 
FineGold
#18 FineGold 2013-01-18 08:53
Thank you. :)
Quote
 
 
Zaher Hicham
#17 Zaher Hicham 2012-10-24 16:20
Merci
Quote
 
 
Peter Drinnan
#16 Peter Drinnan 2012-10-02 01:41
If you're using this on Wamp or Xampp, make sure you open your php.ini file and uncomment the line that shows

extension=php_zip.dll

Once you do that, restart apache and it will work perfectly.
Quote
 
 
renz
#15 renz 2012-09-27 09:08
Thank you :-)
Quote
 
 
Hasanuzzaman Sattar
#14 Hasanuzzaman Sattar 2012-09-24 11:15
Excellent code. Really nice simple code.
Quote
 
 
krishna
#13 krishna 2012-09-12 05:45
very excellent unzip query...................
really that was helpful
once again than you ............... ...........
Quote
 
 
Simon
#12 Simon 2012-09-11 09:25
Great Code!!!!
Quote
 
 
Nidhin Joseph
#11 Nidhin Joseph 2012-08-16 05:11
thanx bro..
Google led me to the right solution by showing your page as the first search result :)
Quote
 
 
Otesto
#10 Otesto 2012-08-13 13:50
Thanks

Very simple and straight to the point.
Quote
 
 
mahdi J
#9 mahdi J 2012-08-10 10:45
thanks
Quote
 
 
romprzy
#8 romprzy 2012-08-01 15:36
thx A LOT m8!
i was looking 4 this piece of code for an hour and i thought it's gonna be long and difficult but it's so simple and good working ;)

cheers!
Quote
 
 
Vig
#7 Vig 2012-07-23 12:07
Thank you :-)
Quote
 
 
Peter
#6 Peter 2012-07-03 01:51
Thank you :-) Often, those little recipes are so helpful.
Quote
 
 
Jorge
#5 Jorge 2012-05-29 00:33
thanks a lot man ;)
Quote
 
 
wdsandeep
#4 wdsandeep 2012-05-28 11:33
thanks... for nice code...
Quote
 
 
Twilight
#3 Twilight 2012-05-16 13:27
Very useful code snippet. Thanks
Quote
 
 
Pacquiao vs Bradley
#2 Pacquiao vs Bradley 2012-05-12 11:08
Very Nice Code, thanks
Quote
 
 
Mike
#1 Mike 2012-04-21 07:18
Short n Sweet code, really very helpful
Thanks a lot
Quote
 

Add comment


Security code
Refresh