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

PHP large file download failure solution

巴扎黑
巴扎黑Original
2018-05-12 14:32:294194browse

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!

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