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

PHP file download

Oct 20, 2016 pm 03:07 PM
php

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;
}

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.


Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.