Unzip Files

$zip = zip_open("zip.zip");
if (is_resource($zip)) {
  while ($zip_entry = zip_read($zip)) {
    $fp = fopen("zip/".zip_entry_name($zip_entry), "w");
    if (zip_entry_open($zip, $zip_entry, "r")) {
      $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));


  1. User Avatar
    Permalink to comment#

    Thanks for the snippet! Awesome site. Very helpful.

  2. User Avatar
    Michael Short
    Permalink to comment#

    Wow, learn something new every day, never knew you could unzip files using PHP :) thanks for the snippet

  3. User Avatar
    Prabhjot Singh
    Permalink to comment#

    Does this script work with simple PHP installed or any particular library or something needed with PHP ?

    • User Avatar
      Umar Farooque

      No, this is as usual and simple programmed code, nothing special needed…

  4. User Avatar
    Permalink to comment#

    i got problem with this message

    Fatal error: Call to undefined function zip_open() in /srv/www/vhosts/mysite.com/httpdocs/unzip.php on line 2

  5. User Avatar
    Permalink to comment#

    Take it easy as:

    and it will unzip file on the server on same file path.

  6. User Avatar
    Jonah Bron
    Permalink to comment#

    If there are directories in the zip file, it will fail. An extra condition is necessary for properly extracting directories.

    $zh = zip_open('file.zip');
    while ($zip_entry = zip_read($zh)) {
    	$name = zip_entry_name($zip_entry);
    	if (substr($name, -1) == '/') {
    		mkdir($destination . $name);
    	} else {
    		$fh = fopen('zip/' . $name, 'w');
    		if (zip_entry_open($zh, $zip_entry, 'r')) {
    			$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
    			fwrite($fh, $buf);
  7. User Avatar
    Sualeh Umer
    Permalink to comment#

    I have came here first time, its an amazing website and i have bookmarked it especially the snippets area!
    I have a small question, mostly we do not find the direct links of .zip files, instead .rar files are very common

    Is there a way to UNRAR the .rar archive on your server?

    Will this work??

    $string="unrar e '$file'";

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.