Php file download class code, php file download processing class

Source: Internet
Author: User
Php file download class code, php file download processing class

  1. /*

  2. * Function: file download
  3. */
  4. /**
  5. * Usage: $ download = new Download ();
  6. * // Set parameters
  7. * $ Download-> set_file_dir ("c :/");
  8. * $ Download-> set_file_name ("boot. ini ");
  9. * $ Download-> set_read_bytes (1024 );
  10. * // File download processing
  11. * $ Download-> deal_with ();
  12. * // Determine whether a file exists
  13. * If ($ download-> is_file_exist ()){
  14. * Echo "file_exist ";
  15. *} Else {
  16. * Echo "file_not_exist ";
  17. *}
  18. */
  19. Class Download {
  20. Private $ file = null; // file Handle
  21. Private $ file_dir = ""; // Directory of the file
  22. Private $ file_name = ""; // file name
  23. Private $ file_exist = false; // indicates whether the file exists. the default value is false.
  24. Private $ read_bytes = 0; // Number of bytes read from the file
  25. Private $ mode = "r"; // file access type

  26. Public function _ construct (){

  27. }

  28. Public function _ destruct (){

  29. If (null! = $ This-> file ){
  30. Fclose ($ this-> file );
  31. }
  32. }

  33. /**

  34. * File download operations
  35. */Bbs.it-home.org
  36. Public function deal_with (){
  37. // Full file path
  38. $ File_path = $ this-> file_dir. $ this-> file_name;

  39. // Check whether the file exists

  40. If (file_exists ($ file_path )){
  41. $ This-> file_exist = true;

  42. // Open the file

  43. $ This-> file = fopen ($ file_path, $ this-> mode );

  44. // Input file tag

  45. Header ("Content-type: application/octet-stream ");
  46. Header ("Accept-Ranges: bytes ");
  47. Header ("Accept-Length:". filesize ($ file_path ));
  48. Header ("Content-Disposition: attachment; filename =". $ this-> file_name );

  49. // Output file content

  50. While (! Feof ($ this-> file ))
  51. {
  52. $ Out = fread ($ this-> file, $ this-> read_bytes );
  53. If (! Get_magic_quotes_gpc ())
  54. {
  55. Echo $ out;
  56. }
  57. Else
  58. {
  59. Echo stripslashes ($ out );
  60. }
  61. }
  62. // Echo fread ($ file, filesize ($ file_dir. $ file_name ));
  63. }
  64. }

  65. /**

  66. * If the returned value is true or false, use the value to determine whether the object exists.
  67. */
  68. Public function is_file_exist (){
  69. Return $ this-> file_exist;
  70. }

  71. /**

  72. * The parameter type is string.
  73. */
  74. Public function set_file_dir ($ file_dir = ""){
  75. $ This-> file_dir = $ file_dir;
  76. }

  77. /**

  78. * The parameter type is string.
  79. */
  80. Public function set_file_name ($ file_name = ""){
  81. $ This-> file_name = $ file_name;
  82. }

  83. /**

  84. * The parameter type is integer.
  85. */
  86. Public function set_read_bytes ($ read_bytes = 1024 ){
  87. $ This-> read_bytes = $ read_bytes;
  88. }
  89. }
  90. ?>

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.