Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  php  >  正文 php header()函数实现文件下载的文件 提示被破坏不能打开

php header()函数实现文件下载的文件 提示被破坏不能打开

发布时间:2018-10-08   编辑:www.jquerycn.cn
jquery中文网为您提供php header()函数实现文件下载的文件 提示被破坏不能打开 等资源,欢迎您收藏本站,我们将为您提供最新的php header()函数实现文件下载的文件 提示被破坏不能打开 资源
最近一客户反映他们网站上所有的图片下载不了了,下载下来的图片都提示文件被破坏,直接导致打不开,作者测试了下发现确实有这个问题,仔细看了下源代码,发现问题的根源在fread这个函数,fread函数的第二个参数是设置读取最大的字节数

经试验发现fread函数单次最大能够读取的字节数是有限制的,仅为8192个字节,即8KB,对于超过这个大小的文件,如果要完整读取,则需要循环读取直至文件结尾。综合以上做了些改正,以下代码是休整之后的代码,经测试问题解决。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4454')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4454>


function download($file_url,$new_name=''){
 if(!isset($file_url)||trim($file_url)==''){
  return '500';
 }
 if(!file_exists($file_url)){//检查文件是否存在
  return '404';
 }
 $file_name=basename($file_url);
 $file_type=explode('.',$file_url);
 $file_type=$file_type[count($file_type)-1];
 $file_name=trim($new_name=='')?$file_name:urlencode($new_name).'.'.$file_type;
 //输入文件标签phpernote
 header("Content-type: application/octet-stream");
 header("Accept-Ranges: bytes");
 header("Accept-Length: ".filesize($file_url));
 header("Content-Disposition: attachment; filename=".$file_name);
 //输出文件内容
 @readfile($file_type);
}

您可能感兴趣的文章:
解决php下载excel无法打开的问题
php header()函数实现文件下载的文件 提示被破坏不能打开
php强制下载指定类型文件的代码
PHP实现文件的下载实例代码
php中header函数的用法举例详解
php header函数 文件下载时直接提示保存的代码
php header头信息应用举例
php导出word文档与excel表格文件
php header函数实现文本文件下载的方法
php强制下载mp3文件的实现代码

[关闭]