douhuang5331 2015-03-24 00:00
浏览 177
已采纳

如果在上传后重命名文件,ZipArchive会抛出错误

If I upload a zip file, and rename it, the ZipArchive function throws an ER_NOZIP error when I try to extract it, whereas if I upload and don't rename it will extract with no issues.

Is this a known issue or is there something I'm doing wrong? Any help would be great!

EDIT for more details: The process is this: I can zip up a folder on my host and download it using ZipArchive just fine via PHP in my browser. Then I have a process where I can upload that zip file and unzip it on my host using ZipArchive. This works fine. The problem seems to come when I download something with the same name and Windows gives it a name like "My_file (2).zip".

When I upload this file, that's when I get the NOZIP error. I used the rename() function to change it's name to remove spaces etc so that it becomes "my_file_2.zip" but it still throws an error. It seems as though only the originally named file will work.

  • 写回答

1条回答 默认 最新

  • dongyinglan8707 2015-03-24 14:44
    关注

    I figured it out after a lot of testing back and forth, though it seems obvious now.

    It seems that when I was downloading the newly created zip from the script/browser, that the header mime type affected the file randomly (not sure why unaltered zip archives worked, but renamed files didn't still).

    Either way, using these headers in the download script seems to have resolved the issue completely:

        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimeType = finfo_file($finfo, $zipDir);
    
        header("Cache-Control: private, max-age=120, must-revalidate");
        header("Pragma: no-cache");
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // long ago
        header("Content-Type: $mimeType");
        header("Content-Transfer-Encoding: Binary");
        header("Content-disposition: attachment; filename=\"" . basename($zipDir) . "\"");
        header("Accept-Ranges: bytes");
        header("Content-Length: " . filesize($zipDir));
    

    Previously the script was using application/octet-stream as the mime type, and changing it to use the proper zip mime appears to fix the issue, so even renaming the file etc doesn't cause errors anymore.

    Hopefully this will also help others who may run into similar issues.

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

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容