PHP Extra Large File download, breakpoint continued download

Source: Internet
Author: User
Tags ranges

Source:

<?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 extensionEcho $sourceFile;if (!ereg ("[Tmp|txt|rar|pdf|doc]", $file _extension)) exit ("Illegal resources download");Detects if a file existsif (!is_file ($sourceFile)) {Die ("<b>404 File not found!</b>"); } $len= FileSize ($sourceFile);Gets the file size $filename= BaseName ($sourceFile);Get the file name $outFile _extension= Strtolower (substr (STRRCHR ($outFile,"."),1));Get file name extensionIndicates the output browser format according to the extensionSwitch ($outFile _extension) {Case"EXE": $ctype="Application/octet-stream";BreakCase"Zip": $ctype="Application/zip";BreakCase"MP3": $ctype="Audio/mpeg";BreakCase"MPG": $ctype="Video/mpeg";BreakCase"Avi": $ctype="Video/x-msvideo";BreakDefault: $ctype="Application/force-download"; }Begin Writing Headers Header ("Cache-control:"); Header"Cache-control:public");Sets 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 ' parameterif (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 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 specifies several ranges: 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 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;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-time connection $size 2 = $size1; 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, 1024x768 * 8)); c20>//output file flush (); //Output buffer Ob_flush ();} Fclose ($FP); Exit ();  

PHP Extra Large File download, breakpoint continued download

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.