Solve the PHP super file download, the breakpoint continues to download the method of detailed _php skills

Source: Internet
Author: User
Tags ranges
There was a problem with a PHP memory overflow during the recent export. The reason is that the download time to read the generated temporary file is too large, PHP memory can not accommodate, an open mind to change the PHP memory limit, but this is only a tactic, so think of another way is to read the file, and download.
The following is the source code:
Copy Code code as follows:

<?php
$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");
Detect if a file exists
if (!is_file ($sourceFile)) {
Die ("<b>404 File not found!</b>");
}
$len = FileSize ($sourceFile); Get File size
$filename = basename ($sourceFile); Get file name
$outFile _extension = Strtolower (substr (STRRCHR ($outFile, "."), 1)); Get file name extension
To indicate 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");
Set Output Browser format
Header ("Content-type: $ctype");
Header ("content-disposition:attachment; Filename= ". $outFile);
Header ("Accept-ranges:bytes");
$size = FileSize ($sourceFile);
If there are $_server[' http_range ' parameters
if (Isset ($_server[' Http_range ')) {
The/*range header field Range header field can request one or more child scopes for an entity.
For example
Represents the first 500 bytes: bytes=0-499
Represents a second 500 byte: bytes=500-999
Represents last 500 bytes: bytes=-500
Represents 500 bytes after range: 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 the range request header, the response is returned in status Code 206 (partialcontent) instead of (OK).
*/
The value of the $_server[' Http_range ' after the breakpoint is reconnected again bytes=4390912-
List ($a, $range) = explode ("=", $_server[' Http_range '));
If yes, download missing part
Str_replace ($range, "-", $range); What's this for?
$size 2 = $size-1; Total number of bytes in file
$new _length = $size 2-$range; Get the next download length
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)) {
To set the maximum execution time for a file
Set_time_limit (0);
Print (Fread ($FP, 1024 * 8)); Output file
Flush (); Output buffering
Ob_flush ();
}
Fclose ($FP);
Exit ();

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.