您的位置:首页 > 编程语言 > PHP开发

记录一个php强制下载文件函数

2016-08-24 09:13 537 查看
整理个经常用的强制下载文件的函数,之前就说把一些常用的东西整理出来结果老是没时间,最近陆续把这些东西整理下。

public function download()
{
$id = $this->_get('id');
$M = M("downloads");
$data=$M->where("id=$id and status=1")->find();
!$data && exit;
$filename = iconv('UTF-8','GBK',$data['filename']);
$savename = $data['savename'];
$myfile = $data[url] ? $data[url] : 'Uploads/file/'.$savename;
if(file_exists($myfile)){
  $M->where("id=$id")->setInc('downloads');
  $file = @ fopen($myfile, "r");
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=" .$filename );
  while (!feof($file)) {
echo fread($file, 50000);
  }
  fclose($file);
  exit;
}else{
  echo '文件不存在!';
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: