我试图用php下载一些文件来隐藏文件路径,但有些文件类型总是被破坏。像pdf和mp3这样的文件类型工作得很好。像doc,ppt,jpg这样的文件类型总是下载损坏。
我使用这些mimetype
if (file_exists($file_real)){
$extension = strtolower(substr(strrchr($file, "."), 1));
switch($extension){
case "ppt": $type = "application/vnd.ms-powerpoint"; break;
case "pdf": $type = "application/pdf"; break; //------ok
case "doc": $type = "application/msword"; break;
case "mp3": $type = "audio/mpeg"; break;//------ok
case "jpg": $type = "image/jpg"; break;
default: $type = "application/force-download"; break;
} 这些头文件
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Content-Type: " . $type);
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=\"" . $header_file . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file_real));
if ($stream = fopen($file_real, 'rb')){
while(!feof($stream) && connection_status() == 0){
set_time_limit(0);
print(fread($stream,1024*8));
flush();
}
fclose($stream);
}发布于 2012-06-03 05:04:54
我的猜测是,您在PHP代码中输出空白字符。可能是换行符。当通过PHP提供二进制文件时,这是文件损坏的一个非常常见的原因,并且很难发现。这也是一些文件类型损坏而其他文件类型没有损坏的典型情况,因为一些文件类型可以处理额外的空白。
只需在源代码中使用<?php和?>标记之外的换行符,就可以很容易地在PHP程序中使用空格。
检查您的PHP程序和所有包含的内容,以确保在程序末尾的结束?>之后没有任何尾随的空行。还要检查文件顶部<?php标记之前的位置,但程序末尾的空行更为常见。
实际上,最好完全删除PHP标记--无论如何它都是可选的,删除它意味着您肯定不会在?>文件的末尾出现任何空白问题。
希望这能有所帮助。
https://stackoverflow.com/questions/10864998
复制相似问题