php header()函数实现文件下载的文件 提示被破坏不能打开
经试验发现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文件的实现代码