php寫的ftp檔案上傳類

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

調用樣本:

  1. /***
  2. * desc:調用樣本
  3. * link:bbs.it-home.org
  4. * date:2013/2/24
  5. */
  6. $ftp = new ftp('192.168.0.249',21,'hlj','123456'); // 開啟FTP串連
  7. $ftp->up_file('aa.wav','test/13548957217/bb.wav'); // 上傳檔案
  8. //$ftp->move_file('aaa/aaa.php','aaa.php');// 移動檔案
  9. //$ftp->copy_file('aaa.php','aaa/aaa.php');// 複製檔案
  10. //$ftp->del_file('aaa.php'); // 刪除檔案
  11. $ftp->close(); // 關閉FTP串連
  12. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.