PHP implementation code for downloading large files and resuming breakpoint downloads
Release: 2016-07-25 08:59:14
Original
929 people have browsed it
-
- $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
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31