简体   繁体   English

PHP readfile()无效的zip

[英]PHP readfile() invalid zip

I have a script that looks up a file in a mysql database, sends out some headers, names the file something human-readable, and then delivers it the the user. 我有一个脚本在mysql数据库中查找文件,发送一些标题,将文件命名为人类可读的,然后将其传递给用户。

It was working for about six months (for thousands of users), until about 6 hours ago. 它工作了大约六个月(成千上万的用户),直到大约6个小时前。 Now, Windows 7 users now get an error that says "Windows can not open the folder. The Compressed (zipped) Folder: '...filename.zip' is invalid." 现在,Windows 7用户现在收到错误消息“Windows无法打开文件夹。压缩(压缩)文件夹:'... filename.zip'无效。” This happens regardless of browser used to download. 无论用于下载的浏览器是什么都会发生 Linux users, etc., can open the delivered files just fine, and so can Win7 users if they have, say, WinRAR. Linux用户等可以很好地打开交付的文件,Win7用户也可以,比如WinRAR。 Only Windows Explorer can't open them. 只有Windows资源管理器无法打开它们。

Here's where it gets really weird... if I make a new copy of my script and remove the various includes that look up the files and decide what to name it, it will download and open just fine... the resulting files from both scripts are identically sized and the resulting name is identical. 这里真的很奇怪...如果我制作了我的脚本的新副本并删除了查找文件的各种包含并决定了它的名称,它将下载并打开就好了......两者生成的文件脚本大小相同,结果名称相同。 [Hardcoding the new name into the original script does not help. [将新名称硬编码到原始脚本中没有帮助。 I kind of thought it might be an encoding issue in the name.] I have tried with a few different zip files with different contents created on different machines by different methods. 我有点认为它可能是名称中的编码问题。]我尝试过使用不同方法在不同机器上创建的不同内容的几个不同的zip文件。

The script checks for headers_sent(), file_exists(), and is_readable(). 该脚本检查headers_sent(),file_exists()和is_readable()。

apache_setenv('no-gzip', '1'); was added to the scripts (with no effect) following the only semi-relevant info I could find here or elsewhere on the internet. 被添加到脚本中(没有任何效果)遵循我在这里或互联网上可以找到的唯一半相关信息。

Clearly, something in the includes is breaking things somehow, but I haven't any idea what to look for... Ideas? 很明显,包含中的某些内容会以某种方式破坏事物,但我不知道该寻找什么......想法? (There are about 300 lines of code in the includes... basically, it's an abstract class for database access, a concrete version of that class for the files and all of their cms info, associated images for the site, etc.) (包含中大约有300行代码...基本上,它是数据库访问的抽象类,文件类的具体版本及其所有cms信息,站点的相关图像等。)

On a whim, I added " ob_clean(); right before the readfile($file); ... it fixed it. So my question now, is: why? Error reporting is off in all of the files. What else could send output but not headers? And why the "sudden" change in behaivor? 一时兴起,我在readfile($file);之前添加了“ ob_clean();它修复了它。所以现在我的问题是:为什么?所有文件中的错误报告都已关闭。还有什么可以发送输出但不是标题?为什么行为的“突然”改变?

Major Issue 重大问题

Have seen this error before and took me time to identify the issues because sometimes its says Header already sent and sometimes it does not. 之前已经看过这个错误,并花了我时间来确定问题,因为有时它会说已经发送了Header,有时却没有。

Here is what i found out : 这是我发现的:

ob_clean works when you discards the contents of the output buffer but if you use ob_ get_ contents before calling ob_clean you would would see the content causing the error ob_clean在丢弃输出缓冲区的内容时起作用,但如果在调用ob_clean之前使用ob_ get_ contents ,则会看到导致错误的内容

To output this information make sure you use var_dump 要输出此信息,请确保使用var_dump

Some other errors 其他一些错误

If there is a space (" " or " \\n" or "\\t") after calling the ?> tag i often get errors that can only be resolved by ob_clean 如果在调用?>标签后有space (" " or " \\n" or "\\t") ,我经常会遇到只能通过ob_clean解决的ob_clean

Open your script look for script that has more lines of empty space after the ?> close tag 打开脚本查找在?> close标记后面有更多空行空间的脚本

Conclusion 结论

I think there is an empty space included in your script you are not seeing and when you copy it .. you are not copying the script but not the spaces that is why it works elsewhere 我认为你的脚本中有一个空的空间,你没有看到,当你复制它时......你不是在复制脚本而是复制空间,这就是为什么它在其他地方工作的原因

Take out the ob_clean(); 取出ob_clean(); and open a damaged file in Wordpad or some hex viewer. 并在Wordpad或一些十六进制查看器中打开一个损坏的文件。 The slightest traces of non-binary output (that includes spaces, commas, E_NOTICEs, periods, dust, bird poop etc) will cripple the ZIP. 最轻微的非二进制输出(包括空格,逗号,E_NOTICE,句号,灰尘,鸟粪等)将破坏ZIP。

At best you'll probably just find some E_NOTICES (Warning: undefined constant x assumed 'x' etc.); 充其量你可能只会发现一些E_NOTICES(警告:未定义常量x假定'x'等); If not, the hosting probably upgraded/changed something. 如果没有,托管可能升级/改变了一些东西。

The next suspect would be that thing which's name eludes me right now. 下一个嫌疑人就是那个名字现在不能包括我的东西。 It's when you have http://url.com/download.php?file=xxx Apache sees .php and sends text/php header. 这时你有http://url.com/download.php?file=xxx Apache看到.php并发送text/php标题。 I don't remember what that was called but you fix it with mod_rewrite. 我不记得那是什么,但你用mod_rewrite修复它。

就像Baba说的那样,没有空格(“”或“\\ n”或“\\ t”),我建议在没有BOM的情况下以UTF8保存php文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM