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

如果我上传zip文件并重命名,ZipArchive函数在我尝试提取时会抛出ER_NOZIP错误 ,如果我上传并且不重命名,它将毫无问题地提取。</ p>

这是一个已知问题还是我做错了什么? 任何帮助都会很棒!</ p>

编辑以获取更多详细信息:</ strong>
过程如下:我可以在主机上压缩文件夹并使用ZipArchive下载它 在我的浏览器中通过PHP很好。 然后我有一个进程,我可以上传该zip文件并使用ZipArchive将其解压缩到我的主机上。 这很好用。 当我下载具有相同名称的内容并且Windows为其命名为“My_file(2).zip”时,问题似乎就出现了。</ p>

当我上传此文件时,就是我得到的时候 NOZIP错误。 我使用rename()函数更改它的名称以删除空格等,使其成为“my_file_2.zip”,但它仍然会抛出错误。 似乎只有最初命名的文件才有效。</ p>
</ div>

展开原文

原文

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个回答



我经过大量的来回测试后想出来,虽然现在看来很明显。</ p>

< p>当我从脚本/浏览器下载新创建的zip时,标题mime类型随机影响了文件(不确定为什么未更改的zip存档工作,但重命名的文件仍然没有)。</ p>

无论哪种方式,在下载脚本中使用这些标题似乎已完全解决了这个问题:</ p>

  $ finfo = finfo_open(FILEINFO_MIME_TYPE); 
$ mimeType = finfo_file($ finfo,$ zipDir);

标题(“Cache-Control:private,max-age = 120,must-revalidate”);
header(“Pragma:no-cache”); \ n header(“Expires:Sat,26 Jul 1997 05:00:00 GMT”); //很久以前
标题(“Content-Type:$ mimeType”);
标题(“Content-Transfer-Encoding:Binary”);
标题(“Content-disposition:attachment; filename = \”“。 basename($ zipDir)。“\”“);
header(”Accept-Ranges:bytes“);
header(”Content-Length:“。filesize($ zipDir));
</ code> < / pre>

以前脚本使用 application / octet-stream </ code>作为mime类型,并且更改它以使用正确的zip mime似乎可以解决问题,所以即使重命名 文件等不再导致错误。</ p>

希望这也可以帮助其他可能遇到类似问题的人。</ p>
</ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐