Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:53:25+00:00 2026-05-15T23:53:25+00:00

Hey everyone, I have written a script that downloads a zip file from a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:53:26+00:00Added an answer on May 15, 2026 at 11:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone. I've written this simple function that is supposed to load a portion
Hey everyone, I have a database result that returns from a method. I need
Hey everyone, I'm working on a PHP application that needs to parse a .tpl
Hey everyone, I have the following 'users' table in MySQL: CREATE TABLE `users` (
Hey everyone, I have achieved the effect described in the title using the following
Hey everyone, I'm a newbie and I have what I anticipate will be a
Hey everyone, here is the site in question: http://www.myvintagesecret.com/ I have a bunch of
Hey everyone, just a quick thing, I have the hex to integer working, but
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey everyone, I am trying to run the following program, but am getting a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.