PHP File download Class code, PHP file Download processing class

Source: Internet
Author: User
Tags fread
  1. /*

  2. * Function: File download
  3. */
  4. /**
  5. * How to use: $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 operation
  11. * $download->deal_with ();
  12. *//Determine if the 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 where the file is located
  22. Private $file _name = "";//File name
  23. Private $file _exist = false;//Indicates whether the file exists, default is not present
  24. Private $read _bytes = 0;//file Read bytes
  25. Private $mode = "R";//access type of file

  26. Public Function __construct () {

  27. }

  28. Public Function __destruct () {

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

  33. /**

  34. * Operation of File download processing
  35. */bbs.it-home.org
  36. Public Function Deal_with () {
  37. File full path
  38. $file _path = $this->file_dir. $this->file_name;

  39. Check if the file exists

  40. if (file_exists ($file _path)) {
  41. $this->file_exist = true;

  42. Open File

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

  44. Input File Label

  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 contents

  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. * The return value is TRUE or FALSE, which determines whether the file exists by its value
  67. */
  68. Public Function is_file_exist () {
  69. return $this->file_exist;
  70. }

  71. /**

  72. * Parameter type is string
  73. */
  74. Public Function Set_file_dir ($file _dir= "") {
  75. $this->file_dir = $file _dir;
  76. }

  77. /**

  78. * Parameter type is string
  79. */
  80. Public Function Set_file_name ($file _name= "") {
  81. $this->file_name = $file _name;
  82. }

  83. /**

  84. * Parameter type is integral type
  85. */
  86. Public Function set_read_bytes ($read _bytes=1024) {
  87. $this->read_bytes = $read _bytes;
  88. }
  89. }
  90. ?>

Copy Code
  • Related Article

    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.