PHP FTP操作類( 上傳、拷貝、移動、刪除檔案/建立目錄 )

來源:互聯網
上載者:User
  1. /**
  2. * 作用:FTP操作類( 拷貝、移動、刪除檔案/建立目錄 )
  3. * 時間:2006/5/9
  4. * 作者:欣然隨風
  5. * QQ:276624915
  6. */
  7. class class_ftp
  8. {
  9. public $off; // 返回操作狀態(成功/失敗)
  10. public $conn_id; // FTP串連
  11. /**
  12. * 方法:FTP串連
  13. * @FTP_HOST -- FTP主機
  14. * @FTP_PORT -- 連接埠
  15. * @FTP_USER -- 使用者名稱
  16. * @FTP_PASS -- 密碼
  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伺服器串連失敗");
  21. @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP伺服器登陸失敗");
  22. @ftp_pasv($this->conn_id,1); // 開啟被動類比
  23. }
  24. /**
  25. * 方法:上傳檔案
  26. * @path -- 本地路徑
  27. * @newpath -- 上傳路徑
  28. * @type -- 若目標目錄不存在則建立
  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 "檔案上傳失敗,請檢查許可權及路徑是否正確!";
  35. }
  36. /**
  37. * 方法:移動檔案
  38. * @path -- 原路徑
  39. * @newpath -- 新路徑
  40. * @type -- 若目標目錄不存在則建立
  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 "檔案移動失敗,請檢查許可權及原路徑是否正確!";
  47. }
  48. /**
  49. * 方法:複製檔案
  50. * 說明:由於FTP無複製命令,本方法變通操作為:下載後再上傳到新的路徑
  51. * @path -- 原路徑
  52. * @newpath -- 新路徑
  53. * @type -- 若目標目錄不存在則建立
  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);// 下載
  59. if(!$this->off) echo "檔案複製失敗,請檢查許可權及原路徑是否正確!";
  60. $this->up_file($downpath,$newpath,$type);
  61. }
  62. /**
  63. * 方法:刪除檔案
  64. * @path -- 路徑
  65. */
  66. function del_file($path)
  67. {
  68. $this->off = @ftp_delete($this->conn_id,$path);
  69. if(!$this->off) echo "檔案刪除失敗,請檢查許可權及路徑是否正確!";
  70. }
  71. /**
  72. * 方法:組建目錄
  73. * @path -- 路徑
  74. */
  75. function dir_mkdirs($path)
  76. {
  77. $path_arr = explode('/',$path); // 取目錄數組
  78. $file_name = array_pop($path_arr); // 彈出檔案名稱
  79. $path_div = count($path_arr); // 取層數
  80. foreach($path_arr as $val) // 建立目錄
  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 "目錄建立失敗,請檢查許可權及路徑是否正確!";
  88. exit;
  89. }
  90. @ftp_chdir($this->conn_id,$val);
  91. }
  92. }
  93. for($i=1;$i=$path_div;$i++) // 回退到根
  94. {
  95. @ftp_cdup($this->conn_id);
  96. }
  97. }
  98. /**
  99. * 方法:關閉FTP串連
  100. */
  101. function close()
  102. {
  103. @ftp_close($this->conn_id);
  104. }
  105. }// class class_ftp end
  106. /************************************** 測試 ***********************************
  107. $ftp = new class_ftp('192.168.100.143',21,'user','pwd'); // 開啟FTP串連
  108. //$ftp->up_file('aa.txt','a/b/c/cc.txt'); // 上傳檔案
  109. //$ftp->move_file('a/b/c/cc.txt','a/cc.txt'); // 移動檔案
  110. //$ftp->copy_file('a/cc.txt','a/b/dd.txt'); // 複製檔案
  111. //$ftp->del_file('a/b/dd.txt'); // 刪除檔案
  112. $ftp->close(); // 關閉FTP串連
  113. ******************************************************************************/
  114. ?>
複製代碼
PHP, FTP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.