Home Backend Development PHP Tutorial Problem analysis of php download files

Problem analysis of php download files

Jul 25, 2016 am 08:57 AM

This article introduces some problems and solutions encountered when downloading files in PHP. Friends in need can refer to them.

When using php to download files, the download box of the browser pops up and the save as operation appears. Sometimes memory overflow and timeout may occur.

If it times out, you can set set_time_limit(0);

If memory overflow occurs, it may be caused by too much data being retrieved from the database.

If a memory overflow occurs when reading from a file, it means that the code reading method is incorrect and it requires calling files or filegetcontens.

If it is fopen, give a buffer with a fixed size, read and then write, and there will be no memory overflow.

Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

<?php

//php下载文件

//by bbs.it-home.org

if (file_exists($file_path)) { //如果文件存在

$handle = fopen($file_path, "r");

while (!feof($handle)) {

$content = fgets($handle, 4096); //读取一行

echo $content; //输出到缓冲区,即php://stdout。

//达到缓冲区设置值后由tcp传给浏览器进行输出,一般到512字节就会通过网络输出给浏览器。

}

fclose($handle);

}

?>

Copy after login

Note: Before output, you need to call @ob_end_flush() once; it cannot be called in a loop, just call it once. @ob_end_flush();//Flush out (send) the contents of the output buffer and close the buffer.

File download: content-type://Download format, if the format cannot be parsed by the browser, the download box will pop up

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?php

//php下载文件

//by bbs.it-home.org

header("Content-Type: application/force-download");

header("Content-Type: application/download");

header("Content-Transfer-Encoding: binary");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Pragma: no-cache");

 

Header("Content-type: application/octet-stream");  //响应内容类型  

Header("Accept-Ranges: bytes");

Header("Accept-Length: ".filesize($filename). ' bytes');

 

Header('Content-Disposition: attachment; filename='.$filename);  //HTTP响应头

?>

Copy after login


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 Article Tags

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)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles