-
- $sourceFile = "jbxue.tmp"; //The name of the temporary file to be downloaded
- $outFile = "User Order.xls"; //The name of the file to be downloaded and saved to the client
- $file_extension = strtolower(substr(strrchr($sourceFile, "."), 1)); //Get the file extension
- //echo $sourceFile;
- if (!ereg("[tmp|txt|rar|pdf|doc] ", $file_extension))exit ("Illegal resource download");
- //Check whether the file exists
- if (!is_file($sourceFile)) {
- die("404 File not found!}
- $len = filesize($sourceFile); //Get the file size
- $filename = basename($sourceFile); //Get the file name
- $outFile_extension = strtolower(substr(strrchr($outFile, " ."), 1)); //Get the file extension
- //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 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'] Parameters
- if (isset ($_SERVER['HTTP_RANGE'])) {
- /*Range header field The Range header field can request one or more sub-ranges of the 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 bytes: bytes=0-0,-1
- Specify several ranges at the same time: bytes=500-600,601-999
- But the server can ignore this request header if the unconditional GET contains Range request header, the response will be returned with status code 206 (PartialContent) instead of 200 (OK).
- */
- // Connect again after the breakpoint. The value of $_SERVER['HTTP_RANGE'] is bytes=4390912-
- list ($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
- //if yes, download missing part
- str_replace($range, "-", $range); //What is this sentence for? . . .
- $size2 = $size -1; //The total number of bytes in the file
- $new_length = $size2 - $range; //Get 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% of the time
- } else {
- //First connection
- $size2 = $size -1;
- header("Content-Range: bytes 0-$size2/$size"); //Content-Range: bytes 0-4988927/4988928
- header("Content-Length: " . $size); //Output total length
- }
- //Open the file
- //edit bbs.it-home.org
- $fp = fopen("$sourceFile", "rb" );
- //Set the 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 buffer
- ob_flush();
- }
- fclose($fp);
- exit ();
- ?>
Copy code
|