PHP large file download, resumable download

Source: Internet
Author: User
Recently, the company encountered a php memory overflow problem when exporting order information. The reason is that the temporary file generated during download is too large to accommodate the PHP memory, when I thought of changing the PHP memory limit, it was just a slow task. so I thought of another way to read and download files in multiple parts. Recently, the company encountered a php memory overflow problem when exporting order information. The reason is that the temporary file generated during download is too large to accommodate the PHP memory, when I thought of changing the PHP memory limit, it was just a slow task. so I thought of another way to read and download files in multiple parts.

The source code is as follows:

 

 
 
  1. $ SourceFile = "1.tmp"; // temporary file name to be downloaded
  2. $ OutFile = "..xls"; // download the file name 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. }
  10. $ Len = filesize ($ sourceFile); // get the file size
  11. $ Filename = basename ($ sourceFile); // get the file name
  12. $ OutFile_extension = strtolower (substr (strrchr ($ outFile, "."), 1); // get the file extension
  13. // Specify the output browser format based on the extension
  14. Switch ($ outFile_extension ){
  15. Case "exe ":
  16. $ Ctype = "application/octet-stream ";
  17. Break;
  18. Case "zip ":
  19. $ Ctype = "application/zip ";
  20. Break;
  21. Case "mp3 ":
  22. $ Ctype = "audio/mpeg ";
  23. Break;
  24. Case "mpg ":
  25. $ Ctype = "video/mpeg ";
  26. Break;
  27. Case "avi ":
  28. $ Ctype = "video/x-msvideo ";
  29. Break;
  30. Default:
  31. $ Ctype = "application/force-download ";
  32. }
  33. // Begin writing headers
  34. Header ("Cache-Control :");
  35. Header ("Cache-Control: public ");
  36. // Set the output browser format
  37. Header ("Content-Type: $ ctype ");
  38. Header ("Content-Disposition: attachment; filename =". $ outFile );
  39. Header ("Accept-Ranges: bytes ");
  40. $ Size = filesize ($ sourceFile );
  41. // If $ _ SERVER ['http _ range'] parameter exists
  42. If (isset ($ _ SERVER ['http _ range']) {
  43. /* The Range header can request one or more sub-ranges of an object.
  44. For example,
  45. Indicates the first 500 bytes: bytes = 0-499
  46. Indicates the second 500 bytes: bytes = 500-999
  47. Indicates the last 500 bytes: bytes =-500
  48. Indicates the range after 500 bytes: bytes = 500-
  49. First and Last bytes: bytes = 0-0,-1
  50. Specify the following ranges: bytes = 500-600,601-999.
  51. However, the server can ignore this request header. if the unconditional GET contains the Range request header, the response will be returned with the status code 206 (PartialContent) instead of 200 (OK ).
  52. */
  53. // The value of connecting to $ _ SERVER ['http _ range'] again after the breakpoint is bytes = 4390912-
  54. List ($ a, $ range) = explode ("=", $ _ SERVER ['http _ range']);
  55. // If yes, download missing part
  56. Str_replace ($ range, "-", $ range); // What is this sentence ....
  57. $ Size2 = $ size-1; // total file bytes
  58. $ New_length = $ size2-$ range; // Obtain the length of the next download.
  59. Header ("HTTP/1.1 206 Partial Content ");
  60. Header ("Content-Length: $ new_length"); // The total Length of input
  61. Header ("Content-Range: bytes $ range $ size2/$ size"); // Content-Range: bytes 4908618-4988927/4988928 95%
  62. } Else {
  63. // The first connection
  64. $ Size2 = $ size-1;
  65. Header ("Content-Range: bytes 0-$ size2/$ size"); // Content-Range: bytes 0-4988927/4988928
  66. Header ("Content-Length:". $ size); // total output Length
  67. }
  68. // Open the file
  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 ();

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.