PHP FTP Operation class (upload, copy, move, delete files/create directory)

Source: Internet
Author: User
Tags file copy ftp connection
  1. /**
  2. * Role: FTP operation class (copy, move, delete files/create directory)
  3. * Time: 2006/5/9
  4. * Author: Gladly with the wind
  5. * qq:276624915
  6. */
  7. Class Class_ftp
  8. {
  9. Public $off; Return operation status (Success/failure)
  10. public $conn _id; FTP connection
  11. /**
  12. * Method: FTP connection
  13. * @FTP_HOST--FTP host
  14. * @FTP_PORT--Port
  15. * @FTP_USER-User name
  16. * @FTP_PASS--Password
  17. */
  18. function __construct ($FTP _host, $FTP _port, $FTP _user, $FTP _pass)
  19. {
  20. $this->conn_id = @ftp_connect ($FTP _host, $FTP _port) or Die ("FTP Server Connection failed");
  21. @ftp_login ($this->conn_id, $FTP _user, $FTP _pass) or Die ("FTP server login failed");
  22. @ftp_pasv ($this->conn_id,1); Turn on passive simulation
  23. }
  24. /**
  25. * Method: Upload File
  26. * @path--Local path
  27. * @newpath--Upload path
  28. * @type-New If the target directory does not exist
  29. */
  30. function Up_file ($path, $newpath, $type =true)
  31. {
  32. if ($type) $this->dir_mkdirs ($newpath);
  33. $this->off = @ftp_put ($this->conn_id, $newpath, $path, ftp_binary);
  34. if (! $this->off) echo "File upload failed, please check the permissions and path is correct!" ";
  35. }
  36. /**
  37. * Method: Move File
  38. * @path-The original path
  39. * @newpath-New Path
  40. * @type-New If the target directory does not exist
  41. */
  42. function Move_file ($path, $newpath, $type =true)
  43. {
  44. if ($type) $this->dir_mkdirs ($newpath);
  45. $this->off = @ftp_rename ($this->conn_id, $path, $newpath);
  46. if (! $this->off) echo "File move failed, please check the permissions and the original path is correct!" ";
  47. }
  48. /**
  49. * Method: Copy File
  50. * Description: Due to FTP no copy command, this workaround is: Download and upload to the new path
  51. * @path-The original path
  52. * @newpath-New Path
  53. * @type-New If the target directory does not exist
  54. */
  55. function Copy_file ($path, $newpath, $type =true)
  56. {
  57. $downpath = "C:/tmp.dat";
  58. $this->off = @ftp_get ($this->conn_id, $downpath, $path, ftp_binary);//download
  59. if (! $this->off) echo "File copy failed, please check the permissions and the original path is correct!" ";
  60. $this->up_file ($downpath, $newpath, $type);
  61. }
  62. /**
  63. * Method: Delete File
  64. * @path--Path
  65. */
  66. function Del_file ($path)
  67. {
  68. $this->off = @ftp_delete ($this->conn_id, $path);
  69. if (! $this->off) echo "File deletion failed, please check the permissions and path is correct!" ";
  70. }
  71. /**
  72. * Method: Generate Directory
  73. * @path--Path
  74. */
  75. function Dir_mkdirs ($path)
  76. {
  77. $path _arr = explode ('/', $path); Fetch directory Array
  78. $file _name = Array_pop ($path _arr); Popup file name
  79. $path _div = count ($path _arr); Number of layers to be taken
  80. foreach ($path _arr as $val)//Create Directory
  81. {
  82. if (@ftp_chdir ($this->conn_id, $val) = = FALSE)
  83. {
  84. $tmp = @ftp_mkdir ($this->conn_id, $val);
  85. if ($tmp = = FALSE)
  86. {
  87. echo "Directory Creation failed, please check the permissions and path is correct!" ";
  88. Exit
  89. }
  90. @ftp_chdir ($this->conn_id, $val);
  91. }
  92. }
  93. for ($i =1; $i = $path _div; $i + +)//fallback to root
  94. {
  95. @ftp_cdup ($this->conn_id);
  96. }
  97. }
  98. /**
  99. * Method: Close FTP connection
  100. */
  101. function Close ()
  102. {
  103. @ftp_close ($this->conn_id);
  104. }
  105. }//class Class_ftp End
  106. /************************************** Test ***********************************
  107. $ftp = new Class_ftp (' 192.168.100.143 ', +, ' user ', ' pwd '); Open FTP Connection
  108. $ftp->up_file (' aa.txt ', ' a/b/c/cc.txt '); Uploading files
  109. $ftp->move_file (' a/b/c/cc.txt ', ' a/cc.txt '); Moving files
  110. $ftp->copy_file (' a/cc.txt ', ' a/b/dd.txt '); Copying files
  111. $ftp->del_file (' a/b/dd.txt '); deleting files
  112. $ftp->close (); Close FTP connection
  113. ******************************************************************************/
  114. ?>
Copy Code
PHP, FTP
  • 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.