Home >Backend Development >PHP Tutorial >PHP large file download failure solution
This article mainly introduces the solution to the failure of PHP readfile to download large files, involving PHP's splitting of large files and the implementation skills of block-by-block download operations. Friends in need can refer to the following
The examples in this article are described Solution to PHP readfile failure to download large files. I share it with you for your reference, the details are as follows:
The large file is more than 200M, but after only 200K is downloaded, it will prompt that the download is complete without reporting an error.
The reason is that PHP has memory limitations and needs to be downloaded in chunks, which meanscut the large file into chunks and then download it chunk by chunk.
if (file_exists($file)) { if (FALSE!== ($handler = fopen($file, 'r'))) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: chunked'); //changed to chunked header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); //header('Content-Length: ' . filesize($file)); //Remove //Send the content in chunks while(false !== ($chunk = fread($handler,4096))) { echo $chunk; } } exit; } echo "<h1>Content error</h1><p>The file does not exist!</p>";
The above is the detailed content of PHP large file download failure solution. For more information, please follow other related articles on the PHP Chinese website!