Downloading jpg,doc,ppt files with php are corrupted(用php下载jpg、doc、ppt文件已损坏)
本文介绍了用php下载jpg、doc、ppt文件已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用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);
}
php>
我的猜测是您在推荐答案代码中输出空白字符。可能是换行符。当通过PHP提供二进制文件时,这是文件损坏的一个非常常见的原因,并且很难发现。这也是一些文件类型损坏而其他文件类型没有损坏的典型情况,因为某些文件类型可以处理额外的空白。
只需在源代码中将换行符放在<?php
和?>
标记之外,就可以非常容易地在PHP程序中使用空格。
?>
之后没有任何尾随空行。还要检查<?php
标记之前的文件顶部,但程序末尾的空行要常见得多。
事实上,最好完全删除?>
结束标记--无论如何它都是可选的,删除它意味着您肯定不会在PHP文件的末尾出现任何空白问题。
希望这会有帮助。
这篇关于用php下载jpg、doc、ppt文件已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:用php下载jpg、doc、ppt文件已损坏


基础教程推荐
猜你喜欢
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01