Home > Backend Development > PHP Problem > What to do if php cannot download large files

What to do if php cannot download large files

藏色散人
Release: 2023-03-05 06:18:01
Original
2912 people have browsed it

Solution to the problem that php cannot download large files: first find and open the "php.ini" file; then set "memory_limit" to 1024M; then output the data to the client browser.

What to do if php cannot download large files

Recommended: "PHP Video Tutorial"

Solve the problem of being unable to download large files using PHP

In actual applications, there will be a size limit when using PHP's fread() function to read server files. When the server file size exceeds a certain amount (128M?), the client browser cannot download the file. Is it because of the memory_limit limit in the configuration file php.ini?

After setting the memory_limit to 1024M, I tested a 180M file and found that the problem remained. Code:

header('content-type:application/octet-stream');
            header('accept-ranges: bytes');
            header('content-length: '.filesize($filePath));
            header('content-disposition:attachment;filename='.$fileName);
            $fp = fopen($filePath, "r");
            echo fread($fp, filesize($filePath));
            fclose($fp);
Copy after login

The above reading method is to output the data to the client browser at one time. Does it have anything to do with this? Try reading from the file line by line:

header('content-type:application/octet-stream');
            header('accept-ranges: bytes');
            header('content-length: '.filesize($filePath));
            header('content-disposition:attachment;filename='.$fileName);
            $fp = fopen($filePath, "r");
            while(!feof($fp)) {
                echo fgets($fp, 4096);
            }
           fclose($fp);
Copy after login

Aha! No problem anymore.

The above is the detailed content of What to do if php cannot download large files. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template