Table of Contents
1. 得到文件路径
2. 设置header信息
3. 输出文件之file_get_contents()方法
4. 输出文件之file()方法
5. 输出文件之readfile()方法
6. 输出文件之fopen()方法
7. 总结
Home Backend Development PHP Tutorial PHP下载文件的方式

PHP下载文件的方式

Jun 23, 2016 pm 01:28 PM

PHP下载文件的方式

1. 得到文件路径

从$_GET['file']得到文件路径

$path_parts = pathinfo($_GET['file']);$file_name  = $path_parts['basename'];$file_path  = '/mysecretpath/' . $file_name;
Copy after login

务必使用上面这种方法得到路径,不能简单的字符串拼接得到路径
$mypath = '/mysecretpath/' . $_GET['file'];
如果输入的是../../,就可以访问任何路径

2. 设置header信息

header('Content-Description: File Transfer'); //描述页面返回的结果header('Content-Type: application/octet-stream'); //返回内容的类型,此处只知道是二进制流。具体返回类型可参考http://tool.oschina.net/commonsheader('Content-Disposition: attachment; filename='.basename($file));//可以让浏览器弹出下载窗口header('Content-Transfer-Encoding: binary');//内容编码方式,直接二进制,不要gzip压缩header('Expires: 0');//过期时间header('Cache-Control: must-revalidate');//缓存策略,强制页面不缓存,作用与no-cache相同,但更严格,强制意味更明显header('Pragma: public');header('Content-Length: ' . filesize($file));//文件大小,在文件超过2G的时候,filesize()返回的结果可能不正确
Copy after login

3. 输出文件之file_get_contents()方法

file_get_contents()把文件内容读取到字符串,也就是要把文件读到内存中,再输出内容

$str = file_get_contents($file);echo $str;
Copy after login

这种方式,只要文件稍微一大,就会超过内存限制

4. 输出文件之file()方法

与file_get_contents()差不多,只不过是file()会把内容按行读取到数组中,也是需要占用内存

$f = file($file);while(list($line, $cnt) = each($f)) {   echo $cnt;}
Copy after login

文件大的时候也会超出内存限制

5. 输出文件之readfile()方法

readfile()方法:读入一个文件并写入到输出缓冲
这种方式可以直接输出到缓冲,不会整个文件占用内存
前提要先清空缓冲,先要让用户看到下载文件的对话框

while (ob_get_level()) ob_end_clean();//设置完header以后ob_clean();flush();  //清空缓冲区readfile($file);
Copy after login

这种方法可以输出大文件,不会因为内存原因导致失败。
但是readfile()同样会造成PHP内存耗尽:http://stackoverflow.com/questions/6627952/why-does-readfile-exhaust-php-memory

PHP has to read the file and it writes to the output buffer. So, for 300Mb file, no matter what the implementation you wrote (by many small segments, or by 1 big chunk) PHP has to read through 300Mb of file eventually.

If multiple user has to download the file, there will be a problem. (In one server, hosting providers will limit memory given to each hosting user. With such limited memory, using buffer is not going to be a good idea. )
I think using the direct link to download a file is a much better approach for big files.

大意:PHP需要都文件,再输出到缓冲。对于一个300M的文件,PHP最终还是要读300M内存。因此在多个用户同时下载的时候,缓冲也会耗尽内存。(不对还请指正)

例如100个用户在下载,就需要100*buffer_size大小的内存

6. 输出文件之fopen()方法

set_time_limit(0);$file = @fopen($file_path,"rb");while(!feof($file)){    print(@fread($file, 1024*8));    ob_flush();    flush();}
Copy after login

fopen()可以读入大文件,每次可以指定读取一部分的内容。在操作大文件的时候也很有用

7. 总结

利用PHP下载文件时,应该要注重场景。如果本身只是几个小文件被下载,那么使用PHP下载比较好;但是如果PHP要承受大量下载请求,这时下载文件就不该交给PHP做。

对于Apache,有mod_xsendfile可以帮助完成下载任务,更简单也更快速

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles