Home > Backend Development > PHP Tutorial > PHP file download class code, PHP file download processing class

PHP file download class code, PHP file download processing class

WBOY
Release: 2016-07-25 08:51:27
Original
1170 people have browsed it
  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 operation
  11. * $download->deal_with();
  12. * //Determine whether 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 = "";//The directory where the file is located
  22. private $file_name = "";//File name
  23. private $file_exist = false;//Indicates whether the file exists, the default is not
  24. private $read_bytes = 0;//Number of bytes read from the file
  25. private $mode = "r";//Access type of the file

  26. public function __construct(){

  27. }

  28. < ;p>public function __destruct(){
  29. if(null != $this->file){
  30. fclose($this->file);
  31. }
  32. }

  33. /* *

  34. * File download processing 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 if 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. * The return value is true or false, and the value is used to determine whether the file exists
  67. */
  68. public function is_file_exist(){
  69. return $this->file_exist;
  70. }

  71. * Parameter type is string
  72. */
  73. public function set_file_dir($file_dir=""){
  74. $this->file_dir = $file_dir;
  75. }

  76. /**

  77. * Parameter type is string
  78. */
  79. public function set_file_name($file_name=""){
  80. $this->file_name = $file_name;
  81. }

  82. /**

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

Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template