Skip to content
March 20, 2008 / ranacse05

Unzip file using PHP


A few days ago i was looking for a script witch one will help me to unzip a file using PHP . There is 2 different ways to do it.One just execute a commend to unzip the file in the shell using PHP and another one is using library functions .Then i found this script from phpxperts.

// create object
$zip = new ZipArchive() ;

// open archive
if ($zip->open(‘archive. zip’) !== TRUE) {
die (‘Could not open archive’);
}

// extract contents to destination directory
$zip->extractTo(‘/ tmp/extracted/ ‘);

// close archive
// print success message

$zip->close();
echo ‘Archive extracted to directory’;

24 Comments

Leave a Comment
  1. manchumahara / Mar 20 2008 4:46 pm

    thanx..

  2. Rumana / Mar 20 2008 4:49 pm

    Thanks to share this stuff
    I think its helpful.

  3. nuhil / Mar 20 2008 7:48 pm

    Simply Nice. Thnx to find out this.

  4. luoskrad / May 20 2008 7:52 pm

    Thanks a lot man, this works just fine with zipped images is great!!

  5. john d / Aug 21 2008 3:58 pm

    yeah nice but wheres the rest!

    Aint there meant to be a class lol

  6. Monster / Sep 24 2008 7:58 am

    Usefull.

  7. sandeep / Dec 22 2008 11:22 am

    thanxx

  8. Ajinkya Shinde / Jan 5 2009 5:10 am

    Thanks for simple script.

  9. Devesh / Jan 13 2009 2:11 pm

    Hi,

    I am using the above code to unzip a zip file. But it shows that Class ‘ZipArchive’ not found.

    Please help me.

  10. Abdul / Apr 30 2009 9:11 am

    Thanks, Very nice, Got after a long search. Thanks

  11. Peter / Jun 13 2009 7:25 pm

    It looks like you need to have php compiled with this library (found here)included. I’d guess that some hosts do this by default, but some (mine included) don’t.

    I’ve found it’s usually easier to just use exec() and have php do it from the command line.

  12. Anita Chitale / Jul 8 2009 10:31 am

    Hi,

    Thank you so much for such a simple script. This is really helpful.

  13. Meindra / Sep 9 2009 10:50 am

    Hi,
    It’s Usefull, thanks. But I am using the above code to unzip a zip file on linux server can’t effect. Please help me.

  14. Gins / Sep 26 2009 5:32 am

    Hi…

    anyone knows how to access a zip file from a server where path to server is given as a http url……i have tride to give path in zip->open …but doesn’t wrks..

    i hope somedbody finds me soln..:)..

  15. Gins / Sep 28 2009 1:27 pm

    Hi….
    atlast soltn found…just use copy keyword…..rest done…:)

  16. Frank / Sep 28 2009 9:27 pm

    Many thanks!

  17. judy / Oct 16 2009 7:16 am

    help

    if php does not have built in zip support using system call to the unzip binarystatus ?

  18. Naeem / Feb 16 2010 1:44 pm

    Nice script, but if worked :(. it shows that Class β€˜ZipArchive’ not found.

  19. Naeem / Feb 16 2010 2:11 pm

    I am using Linux server, and above script didn’t work at all with my case.
    I just want to share a working script of same functionality at : http://www.joretha.com/index.php/other/technical-articles/php-linux-unzip.html

    Hope it will help.

  20. shibsadhan nandy / Feb 25 2010 6:47 am

    Thanks a lot…………………..

    πŸ™‚

  21. Powerweb / Mar 18 2010 1:09 pm

    I am using this code. It’s realy greate.
    Thanks a lot brother.
    πŸ™‚

    • ranacse05 / Mar 18 2010 7:01 pm

      thank u too for ur comment πŸ™‚

  22. onlineflashgamefan / Jun 2 2010 1:19 pm

    Looks easy! =D

    But just 1 question…
    $zip->extractTo(β€˜/ tmp/extracted/ β€˜);

    Can this be: $zip->extractTo(β€˜/public_html/myfolder/ β€˜);
    instead?
    Will they overwrite existing files as well?

    Regards
    Avatar The Last Airbender

  23. Carlos / Apr 19 2011 1:28 pm

    thanks… itΒ΄s very simple…

Leave a reply to Abdul Cancel reply