drd2551 2010-07-28 01:18
浏览 109
已采纳

下载和解压缩zip文件的脚本会返回错误

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
";
          $query .= 'Host: ' . $url_stuff['host'];
          $query .= "

";

          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条回答 默认 最新

  • doulai2025 2010-07-28 01:24
    关注

    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
    ";
    }
    

    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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置