tags:

views:

76

answers:

1

Hey everyone, I have written a script that downloads a zip file from a remote source, and then is supposed to extract the zip file to a directory. Below is the script:

    <?php
        $url = "http://example.com/some_file.zip";
        download($url,'file.zip');

        function download($url,$file_name = NULL){
          if($file_name == NULL){ $file_name = basename($url);}

          $url_stuff = parse_url($url);
          $port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;

          $fp = fsockopen($url_stuff['host'], $port);
          if(!$fp){ return false;}

          $query  = 'GET ' . $url_stuff['path'] . " HTTP/1.0\n";
          $query .= 'Host: ' . $url_stuff['host'];
          $query .= "\n\n";

          fwrite($fp, $query);

          while ($tmp = fread($fp, 8192))   {
            $buffer .= $tmp;
          }

          preg_match('/Content-Length: ([0-9]+)/', $buffer, $parts);
          $file_binary = substr($buffer, - $parts[1]);
          if($file_name == NULL){
            $temp = explode(".",$url);
            $file_name = $temp[count($temp)-1];
          }
          if(!file_exists("packages")){ mkdir("packages", 0755);}
          $file_open = fopen("packages/" . $file_name,'w');
          if(!$file_open){ return false;}
          fwrite($file_open,$file_binary);

          $zip = zip_open(realpath("packages")."/".$file_name);
          if ($zip) {
            while ($zip_entry = zip_read($zip)) {
              $fp = fopen("some_dir/".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);
          }
          fclose($file_open);
          return true;
        }
   ?>

The issue that I have is that while the downloading of the remote file works flawlessly, I can't seem to extract it. The zip_read() and zip_close() return errors saying that it "expects parameter 1 to be resource, integer given...", which I have found means that the zip_open() was unable to extract and is returning an error code, which I have found to be "19" meaning "Zip File Function error: Not a zip archive". However, I know the file I am downloading is, in fact, a zip file. Can anyone explain this odd behavior and provide a fix? It would be much appreciated!

+3  A: 

Quoting php.net: "zip_open() ... Returns a resource handle for later use with zip_read() and zip_close() or returns the number of error if filename does not exist or in case of other error."

This means you cannot test if ($zip) like that. Try

if ( is_resource($zip) ) {
    // stuff
} else {
    print "Zip_open() returned error $zip\n";
}

edit: Apart from that, you need to cut the response in 2 parts properly. You are relying heavily on the Content-Length parameter. You don't check if the preg_match actually matched. A lot of things can go wrong and you should check those things. Try splitting the content on the first empty line (explode on \r\n\r\n or something like that)

Besides the fread() loop should check for feof(), since you would stop reading now if for some reason you would encounter an empty read. Copy&paste from php.net:

while (!feof($handle)) {
    $contents .= fread($handle, 8192);
}

But we can go on and on here. Three main points have to be made:

  • read the fantastic manual (php.net)
  • check return values
  • don't assume you know things you don't

those are related: you must lookup the manual to see what return values you might encounter.

mvds
Way to cut to the chase. :)
George Marian
Thanks, that makes sense and I have made the change, however now instead of printing the errors, it will just skip the extraction part because the zip_open still can't open the zip file. So this didn't solve the problem.
Ben
@ben Then figure out why it's not opening the file. For example, is `realpath("packages")."/".$file_name` the correct path?
George Marian
Well honestly I thought it was something in my code, maybe that it was downloading incorrectly? That's why I included that download section. I think this because I have issues when opening the file with a file manager, it says the zip format is invalid. However I can still open it with 7-zip, so it must be something with the format. I know that its not the original file however, because if I download it on my computer and extract it it works fine. I have also tested it with the wordpress zip file for example, so its not that.
Ben
have you downloaded the file that has been stored by this script? If so, `zip_open` may either just be a little pickier than your average unzip program, or it is a zip format which is not supported by php's `zip_open`.
mvds
Yeah, I guess that must be what it is. Its just pickier/doesn't support the zip format. Thanks for the help and the suggestions on how to clean up my code.
Ben