Home > Backend Development > PHP Tutorial > PHP large file download failure solution

PHP large file download failure solution

巴扎黑
Release: 2023-03-15 06:34:02
Original
4196 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 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>";
Copy after login

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!

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