php强制下载文件函数

yipeiwu_com6年前PHP代码库

本文实例为大家分享了php强制下载文件函数,供大家参考,具体内容如下

public function down()

{
  $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 '文件不存在!';

  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

PHP 日,周,月点击排行统计

复制代码 代码如下: $now=time(); //当前时间 $StrUpdate = "Update $tbl_article set hits=hits+1"; if(date("d...

PHP类与对象中的private访问控制的疑问

复制代码 代码如下: class Bar { public function test() { $this->testPrivate(); $this->testPublic...

使用php判断网页是否gzip压缩

昨天晚上群里有朋友采集网页时发现file_get_contents 获得的网页保存到本地为乱码,响应的header 里 Content-Encoding:gzip但在浏览器里看是正常的。...

PHP小技巧搜集,每个PHPer都来露一手

这个帖子主要是所有的PHPers把自己在开发或学习中的一些经验技巧进行总结,主要就是把解决某种事情更好的方法告诉大家. 我先说几个: 1,假如你使用echo输出一个简单的语句,类似与:...

PHP输出XML格式数据的方法总结

PHP输出XML格式数据的方法总结

本文实例讲述了PHP输出XML格式数据的方法。分享给大家供大家参考,具体如下: 方法1: <?php header("Content-type: text/xml");...