我正在尝试使用 php 下载一些文件以隐藏文件路径,但某些文件类型总是被破坏。pdf 和 mp3 等文件类型效果很好。doc、ppt、jpg 等文件类型总是下载损坏。
我使用这些 mimetypes
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);
}