zero-perfoliate
zero-perfoliate

Author Topic: extract a remote zip file with php  (Read 381 times)

Offline fluid

  • New PHP Members
  • Posts: 3
  • Karma: +0/-0
extract a remote zip file with php
« on: March 11, 2010, 03:03:32 AM »
Hi I am trying to grab a remote zip file then extract it but it is trying to extract it before it has trasnfered so I end up with a complete zip file but an xml file of 0 bytes.  The code I have is:

// read the file
$image = file_get_contents("http://www.examplesite.com/zip");
// write the file
file_put_contents("zips/filename.zip", $image);
// extract the file
$zip = zip_open("zips/filename.zip");
if ($zip) {
  while ($zip_entry = zip_read($zip)) {
    $fp = fopen("zips/".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));
      fwrite($fp,"$buf");
      zip_entry_close($zip_entry);
      fclose($fp);
    }
  }
  zip_close($zip);
}

Many thanks for any help.

 

zero-perfoliate