首页  >  文章  >  类库下载  >  PHP文件下载

PHP文件下载

高洛峰
高洛峰原创
2016-10-20 15:07:541733浏览

完整代码:

public function downloadFile(){
        $M = M($this->tableName);
        $map['id'] = I('fileId');
        $info = $M->where($map)->find();
        $filepath = '.'.$info['path'];
        if( !file_exists($filepath) ){
            echo '文件不存在!';
            exit;
        }
 
        //$M->where($map)->setInc('download');
        $file = fopen($filepath,"r"); // 打开文件
        // 输入文件标签
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length: ".filesize($filepath));
        Header("Content-Disposition: attachment; filename=" . $info['savename']);
        // 输出文件内容
        echo fread($file,filesize($filepath));
        fclose($file);
        exit;
}

以上代码为thinkphp下的方法


$this->tableName 为存储文件的数据表

根据fileId获取数据库响应文件

通过file_exists判断文件是否存在,如果不存在则输出错误信息


通过fopen打开文件

在更具Header方法处理打开文件,以及通过fread()方法把文件输出。


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关文章

查看更多