PHP Extra Large File download, breakpoint continued download

Source: Internet
Author: User
Tags ranges
PHP Extra Large File download, breakpoint continued download

Export order information when 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:

  $sourceFile = "1.tmp"; 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 extension//echo $sourceFile;  if (!ereg ("[Tmp|txt|rar|pdf|doc]", $file _extension)) exit ("Illegal resource Download");  Detects if the file exists if (!is_file ($sourceFile)) {die ("404 File Not found!"); } $len = FileSize ($sourceFile); Gets the file size $filename = basename ($sourceFile); Get the file name $outFile _extension = Strtolower (substr (STRRCHR ($outFile, "."), 1); Get file name extension//Indicate output browser format according to 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 the Output browser format header ("Content-type: $ctype"); Header ("content-disposition:attachment; Filename= ".  $outFile);  Header ("Accept-ranges:bytes");  $size = FileSize ($sourceFile);      If there is $_server[' http_range '] parameter if (isset ($_server[' Http_range ')) {/*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 the second 500 bytes: 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 but the server can ignore this request header, and if the unconditional get contains a range request header, the response is 206 with a status code (part      Ialcontent) returns instead of a (OK).       *///breakpoint after connecting again $_server[' Http_range ' value 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 Files bytes $new _length = $size 2-$range;      Gets the length of the next download header ("http/1.1 206 Partial Content"); Header ("Content-length: $new _length"); //Enter the total length header ("Content-range:bytes $range $size2/$size");      Content-range:bytes 4908618-4988927/4988928 95%} else {//first connection $size 2 = $size-1; Header ("Content-range:bytes 0-$size 2/$size"); Content-range:bytes 0-4988927/4988928 Header ("Content-length:". $size);  Output total length}//Open File $fp = fopen ("$sourceFile", "RB");  Set pointer position fseek ($FP, $range);      Unreal Output while (!feof ($fp)) {//Set file Maximum execution time set_time_limit (0); Print (Fread ($FP, 1024 * 8)); Output file flush ();  Output buffer Ob_flush ();  } fclose ($FP);  Exit ();
  • 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.