Home > 类库下载 > PHP类库 > PHP file download

PHP file download

高洛峰
Release: 2016-10-20 15:07:54
Original
1806 people have browsed it

Complete code:

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;
}
Copy after login

The above code is the method under thinkphp


$this->tableName is the data table for storing files

Get the database response file based on fileId

Use file_exists to determine whether the file exists. If it does not exist, Then the error message is output


Open the file through fopen

The more Header method handles opening the file, and the file is output through the fread() method.


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template