Php security code for downloading large files

Source: Internet
Author: User
Php security code for downloading large files

  1. /**
  2. * Secure download of general files
  3. * Edit bbs.it-home.org
  4. */
  5. $ Durl = 'File/phpcms2008_o2abf32efj883c91a.iso ';
  6. $ Filename = 'phpcms2008 _ o2abf32efj883c91a. iso ';
  7. $ File = @ fopen ($ durl, 'r ');
  8. Header ("Content-Type: application/octet-stream ");
  9. Header ("Accept-Ranges: bytes ");
  10. Header ("Accept-Length:". filesize ($ durl ));
  11. Header ("Content-Disposition: attachment; filename =". $ filename );
  12. Echo fread ($ file, filesize ($ durl ));
  13. Fclose ($ file );
  14. ?>

The above code exceeded php in the case of large files. when the file with the maximum memory configured by ini is used, the server will occupy a lot of CPU resources. files cannot be downloaded normally, and only dozens of Kb of files can be downloaded. You can use the following code to solve the problem:

  1. /**
  2. * Implementation code for secure download of large files
  3. * Edit bbs.it-home.org
  4. */
  5. Function download ($ url, $ filename ){
  6. // Obtain the file size to prevent files exceeding 2 GB from being read by sprintf.
  7. $ Filesize = sprintf ("% u", filesize ($ url ));
  8. If (! $ Filesize ){
  9. Return;
  10. }
  11. Header ("Content-type: application/octet-stream \ n"); // application/octet-stream
  12. Header ("Content-type: unknown/unknown ;");
  13. Header ("Content-disposition: attachment; filename = \" ". $ filename ."\"");
  14. Header ('content-transfer-encoding: binary ');
  15. If ($ range = getenv ('http _ range') {// when there is an offset, use the 206 breakpoint resume header.
  16. $ Range = explode ('=', $ range );
  17. $ Range = $ range [1];
  18. Header ("HTTP/1.1 206 Partial Content ");
  19. Header ("Date:". gmdate ("D, d m y h: I: s"). "GMT ");
  20. Header ("Last-Modified:". gmdate ("D, d m y h: I: s", filemtime ($ url). "GMT ");
  21. Header ("Accept-Ranges: bytes ");
  22. Header ("Content-Length:". ($ filesize-$ range ));
  23. Header ("Content-Range: bytes". $ range. ($ filesize-1). "/". $ filesize );
  24. Header ("Connection: close". "\ n ");
  25. } Else {
  26. Header ("Content-Length:". $ filesize. "\ n ");
  27. $ Range = 0;
  28. }
  29. LoadFile ($ url );
  30. }
  31. Function loadFile ($ filename, $ retbytes = true ){
  32. $ Buffer = '';
  33. $ Cnt = 0;
  34. $ Handle = fopen ($ filename, 'RB ');
  35. If ($ handle = false ){
  36. Return false;
  37. }
  38. While (! Feof ($ handle )){
  39. $ Buffer = fread ($ handle, 1024*1024 );
  40. Echo $ buffer;
  41. Ob_flush ();
  42. Flush ();
  43. If ($ retbytes ){
  44. $ Cnt + = strlen ($ buffer );
  45. }
  46. }
  47. $ Status = fclose ($ handle );
  48. If ($ retbytes & $ status ){
  49. Return $ cnt; // return num. bytes delivered like readfile () does.
  50. }
  51. Return $ status;
  52. }
  53. ?>

Call example:

  1. // Secure download of large files
  2. Download ($ url, $ filename );

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.