PHP solution to readfile failure to download large files

墨辰丷
Release: 2023-03-27 12:20:02
Original
2455 people have browsed it

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>";
Copy after login



##Related recommendations:

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!