Home >Backend Development >PHP Tutorial >FTP file upload class written in php

FTP file upload class written in php

WBOY
WBOYOriginal
2016-07-25 09:04:39963browse
  1. /**
  2. * desc:FTP类
  3. * link:bbs.it-home.org
  4. * date:2013/02/24
  5. */
  6. class ftp
  7. {
  8. public $off; // Return operation status (success/failure)
  9. public $conn_id; // FTP connection
  10. / **
  11. * Method: FTP connection
  12. * @FTP_HOST -- FTP host
  13. * @FTP_PORT -- Port
  14. * @FTP_USER -- Username
  15. * @FTP_PASS -- Password
  16. */
  17. function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS)
  18. {
  19. $this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("FTP server connection failed ");
  20. @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP server login failed");
  21. @ftp_pasv($this->conn_id,1); // Turn on passive simulation
  22. }
  23. /**
  24. * Method: Upload file
  25. * @path-- local path
  26. * @newpath -- upload path
  27. * @type-- create a new directory if it does not exist
  28. */
  29. function up_file($path,$newpath,$type=true)
  30. {
  31. if($type) $this->dir_mkdirs($newpath);
  32. $this- >off = @ftp_put($this->conn_id,$newpath,$path,FTP_BINARY);
  33. if(!$this->off) echo "File upload failed, please check whether the permissions and path are correct!";
  34. }
  35. /**
  36. * Method: Move files
  37. * @path-- original path
  38. * @newpath -- new path
  39. * @type-- create a new directory if it does not exist
  40. */
  41. function move_file($path,$newpath,$type=true)
  42. {
  43. if($type) $this->dir_mkdirs($newpath);
  44. $this- >off = @ftp_rename($this->conn_id,$path,$newpath);
  45. if(!$this->off) echo "File move failed, please check whether the permissions and original path are correct!";
  46. }
  47. /**
  48. * Method: Copy files
  49. * Note: Since FTP does not have a copy command, the alternative operation of this method is: download and then upload to a new path
  50. * @path-- original path
  51. * @newpath -- new path
  52. * @type -- If the target directory does not exist, create it
  53. */
  54. function copy_file($path,$newpath,$type=true)
  55. {
  56. $downpath = "c:/tmp.dat";
  57. $this->off = @ ftp_get($this->conn_id,$downpath,$path,FTP_BINARY);// Download
  58. if(!$this->off) echo "File copy failed, please check whether the permissions and original path are correct! ";
  59. $this->up_file($downpath,$newpath,$type);
  60. }
  61. /**
  62. * Method: delete file
  63. * @path -- path
  64. */
  65. function del_file($path)
  66. {
  67. $this->off = @ ftp_delete($this->conn_id,$path);
  68. if(!$this->off) echo "File deletion failed, please check whether the permissions and path are correct! ";
  69. }
  70. /**
  71. * Method: Generate directory
  72. * @path -- path
  73. */
  74. function dir_mkdirs($path)
  75. {
  76. $path_arr = explode('/',$path); // Get directory array
  77. $file_name = array_pop($path_arr );// Pop up file name
  78. $path_div = count($path_arr);// Get the number of layers
  79. foreach($path_arr as $val)// Create directory
  80. {
  81. if(@ftp_chdir($this->conn_id ,$val) == FALSE)
  82. {
  83. $tmp = @ftp_mkdir($this->conn_id,$val);
  84. if($tmp == FALSE)
  85. {
  86. echo "Directory creation failed, please check permissions and Is the path correct? ";
  87. exit;
  88. }
  89. @ftp_chdir($this->conn_id,$val);
  90. }
  91. }
  92. for($i=1;$i<=$path_div;$i++) // Fallback to root
  93. {
  94. @ftp_cdup($this->conn_id);
  95. }
  96. }
  97. /**
  98. * Method: Close FTP connection
  99. */
  100. function close()
  101. {
  102. @ftp_close($this->conn_id);
  103. }
  104. }
  105. // class class_ftp end
  106. ?>
Copy code

Call example:

  1. /***
  2. * desc: calling example
  3. * link: bbs.it-home.org
  4. * date: 2013/2/24
  5. */
  6. $ftp = new ftp('192.168.0.249',21,'hlj','123456'); // Open FTP connection
  7. $ftp ->up_file('aa.wav','test/13548957217/bb.wav'); //Upload file
  8. //$ftp->move_file('aaa/aaa.php','aaa.php') ;//Move files
  9. //$ftp->copy_file('aaa.php','aaa/aaa.php');// Copy files
  10. //$ftp->del_file('aaa.php') ; // Delete file
  11. $ftp->close(); // Close FTP connection
  12. ?>
Copy code


Statement:
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