php强制下载文件函数

yipeiwu_com5年前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自定session保存路径及删除、注销与写入的方法

本文实例讲述了PHP自定session保存路径及删除、注销与写入的方法。分享给大家供大家参考。具体方法如下: 复制代码 代码如下: $sessionpath=session_save_p...

PHP将回调函数作用到给定数组单元的方法

数组是PHP程序设计中十分重要的一环。本文介绍PHP中数组函数array_map()的用法,实现将回调函数作用到给定数组单元上。具体如下: array array_map ( cal...

给PHP开发者的编程指南 第一部分降低复杂程度

PHP 是一门自由度很高的编程语言。它是动态语言,对程序员有很大的宽容度。作为 PHP 程序员,要想让你的代码更有效,需要了解不少的规范。很多年来,我读过很多编程方面的书籍,与很多资深程...

php压缩多个CSS为一个css的代码并缓存

复制代码 代码如下: <?php /* Compress multiple CSS files into one and cache for an hour. Use the sa...

php set_time_limit()函数的使用详解

语法 : void set_time_limit (int seconds)说明 : 设定一个程式所允许执行的秒数,如果到达限制的时间,程式将会传回错误。它预设的限制时间是30秒,max...