To solve php large file download, Breakpoint continued download Method _php tutorial

Source: Internet
Author: User
Tags ranges
The recent export of a PHP memory overflow problem, the reason is that the download time to read the generated temporary file is too large, PHP memory can not be accommodated, in the thought of changing the PHP memory limit, but this is only a delaying tactic, so think of another way is to read the file, and download.
here is the source code:
Copy CodeThe code is as follows:
$sourceFile = "1.tmp"; The temporary file name to download
$outFile = "user order. xls"; Download the file name saved to the client
$file _extension = Strtolower (substr (STRRCHR ($sourceFile, "."), 1); Get file name extension
Echo $sourceFile;
if (!ereg ("[Tmp|txt|rar|pdf|doc]", $file _extension)) exit ("Illegal resource Download");
Detects if a file exists
if (!is_file ($sourceFile)) {
Die (" 404 File Not found!");
}
$len = FileSize ($sourceFile); Get File size
$filename = basename ($sourceFile); Get file name
$outFile _extension = Strtolower (substr (STRRCHR ($outFile, "."), 1); Get file name extension
Indicates the output browser format according to the extension
Switch ($outFile _extension) {
Case "EXE":
$ctype = "Application/octet-stream";
Break
Case "Zip":
$ctype = "Application/zip";
Break
Case "MP3":
$ctype = "Audio/mpeg";
Break
Case "MPG":
$ctype = "Video/mpeg";
Break
Case "avi":
$ctype = "Video/x-msvideo";
Break
Default:
$ctype = "Application/force-download";
}
Begin Writing Headers
Header ("Cache-control:");
Header ("Cache-control:public");
Setting the Output browser format
Header ("Content-type: $ctype");
Header ("content-disposition:attachment; Filename= ". $outFile);
Header ("Accept-ranges:bytes");
$size = FileSize ($sourceFile);
If there is a $_server[' http_range ' parameter
if (Isset ($_server[' Http_range ')) {
The/*range header field Range header field can request one or more child ranges of an entity.
For example
Represents the first 500 bytes: bytes=0-499
Represents a second 500 byte: bytes=500-999
Represents the last 500 bytes: bytes=-500
Represents the range after 500 bytes: bytes=500-
First and last byte: Bytes=0-0,-1
Specify several ranges at the same time: bytes=500-600,601-999
However, the server can ignore this request header, and if the unconditional get contains a range request header, the response is returned as a status code of 206 (partialcontent) instead of a (OK).
*/
The value of $_server[' Http_range '] is connected again after the breakpoint bytes=4390912-
List ($a, $range) = explode ("=", $_server[' Http_range ');
If yes, download missing part
Str_replace ($range, "-", $range); What does that say?
$size 2 = $size-1; Total number of files bytes
$new _length = $size 2-$range; Get the length of the next download
Header ("http/1.1 206 Partial Content");
Header ("Content-length: $new _length"); Input total length
Header ("Content-range:bytes $range $size2/$size"); When Content-range:bytes 4908618-4988927/4988928 95%,
} else {
First time connection
$size 2 = $size-1;
Header ("Content-range:bytes 0-$size 2/$size"); Content-range:bytes 0-4988927/4988928
Header ("Content-length:".) $size); Total output length
}
Open File
$fp = fopen ("$sourceFile", "RB");
Set pointer position
Fseek ($fp, $range);
Unreal output
while (!feof ($fp)) {
Set the maximum file execution time
Set_time_limit (0);
Print (Fread ($FP, 1024 * 8)); Output file
Flush (); Output buffering
Ob_flush ();
}
Fclose ($FP);
Exit ();

http://www.bkjia.com/PHPjc/327461.html www.bkjia.com true http://www.bkjia.com/PHPjc/327461.html techarticle The recent export of a PHP memory overflow problem, the reason is that the download time to read the generated temporary file too large, PHP memory can not be accommodated, as the idea of changing PHP inside ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.