Home >Backend Development >PHP Tutorial >PHP implementation code for downloading large files and resuming breakpoint downloads

PHP implementation code for downloading large files and resuming breakpoint downloads

WBOY
WBOYOriginal
2016-07-25 08:59:14928browse
  1. $sourceFile = "jbxue.tmp"; //The name of the temporary file to be downloaded
  2. $outFile = "User Order.xls"; //The name of the file to be downloaded and saved to the client
  3. $file_extension = strtolower(substr(strrchr($sourceFile, "."), 1)); //Get the file extension
  4. //echo $sourceFile;
  5. if (!ereg("[tmp|txt|rar|pdf|doc] ", $file_extension))exit ("Illegal resource download");
  6. //Check whether the file exists
  7. if (!is_file($sourceFile)) {
  8. die("404 File not found!}
  9. $len = filesize($sourceFile); //Get the file size
  10. $filename = basename($sourceFile); //Get the file name
  11. $outFile_extension = strtolower(substr(strrchr($outFile, " ."), 1)); //Get the file extension
  12. //Indicate the output browser format according to the extension
  13. switch ($outFile_extension) {
  14. case "exe" :
  15. $ctype = "application/octet-stream";
  16. break;
  17. case "zip" :
  18. $ctype = "application/zip";
  19. break;
  20. case "mp3" :
  21. $ctype = "audio/mpeg";
  22. break;
  23. case "mpg" :
  24. $ctype = "video/mpeg";
  25. break;
  26. case "avi" :
  27. $ctype = "video/x-msvideo";
  28. break;
  29. default :
  30. $ctype = "application/force-download";
  31. }
  32. / /Begin writing headers
  33. header("Cache-Control:");
  34. header("Cache-Control: public");
  35. //Set the output browser format
  36. header("Content-Type: $ctype");
  37. header ("Content-Disposition: attachment; filename=" . $outFile);
  38. header("Accept-Ranges: bytes");
  39. $size = filesize($sourceFile);
  40. //If there is $_SERVER['HTTP_RANGE'] Parameters
  41. if (isset ($_SERVER['HTTP_RANGE'])) {
  42. /*Range header field The Range header field can request one or more sub-ranges of the entity.
  43. For example,
  44. represents the first 500 bytes: bytes=0-499
  45. represents the second 500 bytes: bytes=500-999
  46. represents the last 500 bytes: bytes=-500
  47. represents the range after 500 bytes :bytes=500-
  48. First and last bytes: bytes=0-0,-1
  49. Specify several ranges at the same time: bytes=500-600,601-999
  50. 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).
  51. */
  52. // Connect again after the breakpoint. The value of $_SERVER['HTTP_RANGE'] is bytes=4390912-
  53. list ($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
  54. //if yes, download missing part
  55. str_replace($range, "-", $range); //What is this sentence for? . . .
  56. $size2 = $size -1; //The total number of bytes in the file
  57. $new_length = $size2 - $range; //Get the length of the next download
  58. header("HTTP/1.1 206 Partial Content");
  59. header( "Content-Length: $new_length"); //Enter the total length
  60. header("Content-Range: bytes $range$size2/$size"); //Content-Range: bytes 4908618-4988927/4988928 95% of the time
  61. } else {
  62. //First connection
  63. $size2 = $size -1;
  64. header("Content-Range: bytes 0-$size2/$size"); //Content-Range: bytes 0-4988927/4988928
  65. header("Content-Length: " . $size); //Output total length
  66. }
  67. //Open the file
  68. //edit bbs.it-home.org
  69. $fp = fopen("$sourceFile", "rb" );
  70. //Set the pointer position
  71. fseek($fp, $range);
  72. //Unreal output
  73. while (!feof($fp)) {
  74. //Set the maximum file execution time
  75. set_time_limit(0);
  76. print (fread($fp, 1024 * 8)); //Output file
  77. flush(); //Output buffer
  78. ob_flush();
  79. }
  80. fclose($fp);
  81. exit ();
  82. ?>
Copy code


Statement:
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