Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php下载jpg、doc、ppt文件已损坏_Php_Mime Types_Content Type - Fatal编程技术网

用php下载jpg、doc、ppt文件已损坏

用php下载jpg、doc、ppt文件已损坏,php,mime-types,content-type,Php,Mime Types,Content Type,我试图用php下载一些文件来隐藏文件路径,但有些文件类型总是被破坏。 像pdf和mp3这样的文件类型非常有用。 像doc、ppt、jpg这样的文件类型总是被破坏 我用这些模版 if (file_exists($file_real)){ $extension = strtolower(substr(strrchr($file, "."), 1)); switch($extension){ case "ppt": $type = "application/vnd.ms-powerpoint";

我试图用php下载一些文件来隐藏文件路径,但有些文件类型总是被破坏。 像pdf和mp3这样的文件类型非常有用。 像doc、ppt、jpg这样的文件类型总是被破坏

我用这些模版

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);
        }

我猜您是在PHP代码中输出空白字符。可能是线路馈线。这是通过PHP提供二进制文件时导致文件损坏的一个非常常见的原因,并且很难发现。这也是一些文件类型损坏而其他文件类型未损坏的典型情况,因为某些文件类型可以处理额外的空白

只需在源代码中的
标记之外添加换行符,空白就可以很容易地进入PHP程序

检查您的PHP程序以及所有包含的内容,以确保在程序结束时关闭
?>
后,它们没有任何尾随的空行。另外,在
结束标记之前检查文件的顶部——不管怎样,它是可选的,删除它意味着您的PHP文件末尾绝对不会有任何空白问题


希望有帮助。

您正在下载PPT/DOC还是PPTX/DOCX?DocX内容类型为“application/vnd.openxmlformats officedocument.wordprocessingml.document”,而PPTX为“application/vnd.openxmlformats officedocument.presentationml.presentation”您以何种方式打开和输出文件?使用word2003创建的Doc文件和使用powerpoint2003.Tested创建的powerpoint文件。删除?>清除空白,但仍然存在相同的问题。您是否将原始文件与下载的副本进行了字节比较?这可能会给你一个关于它是什么的线索。下载的文档文件有66.051字节,原始的66.051字节。045bytes@Allos你能比较一下这些文件,看看这六个字节是什么吗?文件的开头或结尾是空格还是空白?或者他们是其他类型的腐败?如果是前者,则需要进一步查找代码中的空白。如果是别的问题,那么问题就更复杂了;但是我在代码中看不到任何可以这样做的东西。