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

更新时间:2019-05-17    来源:php函数    手机版     字体:

【www.bbyears.com--php函数】

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

 代码如下


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

本文来源:http://www.bbyears.com/jiaocheng/51510.html