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 details are as follows:
The large file is more than 200M, but after downloading only 200K, it prompts 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>";
php中readfile() Function method to set file size
php readfile() Modify file upload size case
readfile() function method to set php file size
The above is the detailed content of PHP solution to readfile failure to download large files. For more information, please follow other related articles on the PHP Chinese website!