如何修复此PHP下载脚本,该脚本损坏了文件?
我有一个强制下载脚本,该脚本可以用PDF和纯文本生成良好的结果,并且对ZIP存档是半确定的(它们在Windows中工作,而不在Linux中工作).但是,应用程序文件和图像全部失败.这些构成了我必须处理的绝大多数文件.正如我在此处针对类似主题所建议的那样,压缩所有下载内容不是一种选择.
I have a force-download script that produces good results with PDF and plain text, and is semi-OK with ZIP archives (they work in Windows, not in Linux). However, application files and images all fail. These make up the vast majority of the files I must handle. Zipping all downloads, as I've seen suggested on similar topics here, is not an option.
失败的文件将下载到完整大小,并以正确的名称写入磁盘.尝试打开它们会导致错误消息,该错误消息因类型而异.将下载的文件与 hexdump 中的原始文件进行比较,可以看到该脚本在每个下载文件的开头插入了以下字符:
The failing files download to their full size, and are written to disk under the correct name. Attempts to open them result in a error message, which differs between types. Comparing downloaded files to their originals in hexdump, I can see that the script inserts the following characters at the start of each downloaded file:
ef bb bf
下载的文件然后会复制原始文件,直到其停止在其指定大小为止-因此原始文件的最后6个字符始终会丢失.
The downloaded file then reproduces the original until it stops at its specified size - so the original's last 6 characters are always missing.
不幸的是,我对二进制文件的组成方式,这些字符的含义或脚本插入方式/原因一无所知.
Unfortunately I know nothing about how binary files are made up, what these characters might mean, or how/why the script is inserting them.
这是按原样的脚本:
$file = '94.ppt';
$path = $_SERVER['DOCUMENT_ROOT']."/relative/path/";
$full_path = $path.$file;
if ($fd = fopen ($full_path, "r")) {
$fsize = filesize($full_path);
$path_parts = pathinfo($full_path);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "txt":
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "jpg":
header("Content-type: image/jpeg");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
case "ppt":
header("Content-Type: application/vnd.ms-powerpoint");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
}
header("Content-Transfer-Encoding: binary");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
开发系统是Apache 2.2.14(Ubuntu)上的PHP 5.3.2-1.生产主机是Apache 2.0.63(某种类型的Linux)上的PHP 5.2.9.
The development system is PHP 5.3.2-1 on Apache 2.2.14 (Ubuntu). The production host is PHP 5.2.9 on Apache 2.0.63 (some type of Linux).