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

What to do if php cannot download large files

藏色散人
藏色散人Original
2020-08-22 10:49:432873browse

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

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

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!

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