歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網

php ftp類

日期:2017/3/1 10:50:48   编辑:Linux編程
  1. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  2. <?php
  3. /*
  4. php ftp類主要功能:
  5. 1.連接ftp並登陸;
  6. 2.創建目錄和刪除目錄;
  7. 3.上傳文件和刪除文件;
  8. */
  9. include 'config.php';
  10. class Net_FTP {
  11. var $ftp_server;
  12. var $ftp_user;
  13. var $ftp_pass;
  14. var $ftp_port;
  15. var $conn_id;
  16. function Net_FTP() {
  17. $this->ftp_server = server;
  18. $this->ftp_user = username;
  19. $this->ftp_pass = password;
  20. $this->ftp_port = port;
  21. // 建立連接
  22. $this->conn_id = ftp_connect($this->ftp_server, $this->ftp_port) or die("不能夠連接到 $this->ftp_server");
  23. // 嘗試登陸
  24. if (!ftp_login($this->conn_id, $this->ftp_user, $this->ftp_pass))
  25. {
  26. $this->message("連接失敗 $this->ftp_user");
  27. }
  28. else
  29. {
  30. $this->message("連接成功 $this->ftp_user ");
  31. }
  32. }
  33. //功能:創建新的目錄
  34. //$path默認為空目錄
  35. //創建成功返回true,否則返回false。
  36. function newdir($path = null)
  37. {
  38. if($this->ftp_is_dir($this->conn_id,$path)||@ftp_mkdir($this->conn_id,$path))
  39. return true;
  40. if(!$this->newdir(dirname($path)))
  41. return false;
  42. ftp_mkdir($this->conn_id,$path);
  43. return true;
  44. }
  45. //驗證是否為目錄
  46. //對$path進行驗證:如果是目錄返回true,否則返回false。
  47. function ftp_is_dir($path)
  48. {
  49. $original_directory = ftp_pwd($this->conn_id);
  50. if(@ftp_chdir($this->conn_id,$path))
  51. {
  52. ftp_chdir($this->conn_id,$original_directory);
  53. return true;
  54. }
  55. else
  56. return false;
  57. }
  58. //功能:上傳文件
  59. //$ftppath:存在ftp服務器位置;$localpath:本地文件位置;
  60. //上傳成功返回true,否則返回false。
  61. function uploadfile($ftppath = null, $localpath = null)
  62. {
  63. if(!emptyempty($ftppath) && !emptyempty($localpath))
  64. {
  65. $ret = ftp_nb_put($this->conn_id, $ftppath, $localpath, FTP_BINARY);
  66. while ($ret == FTP_MOREDATA)
  67. {
  68. $ret = ftp_nb_continue ($this->conn_id);
  69. }
  70. if ($ret != FTP_FINISHED)
  71. {
  72. $this->message( "上傳失敗");
  73. return false;
  74. }
  75. else
  76. {
  77. $this->message("上傳成功");
  78. return true;
  79. }
  80. }
  81. }
  82. //功能:刪除目錄
  83. //$dir:要刪除的目錄
  84. //刪除成功返回true,否則返回false。
  85. function deldir($dir = null)
  86. {
  87. if (ftp_rmdir($this->conn_id, $dir))
  88. {
  89. $this->message("刪除目錄成功");
  90. return true;
  91. }
  92. else
  93. {
  94. $this->message("刪除目錄失敗");
  95. return false;
  96. }
  97. }
  98. //功能:返回目錄
  99. //返回當前目錄名稱
  100. function redir()
  101. {
  102. return ftp_pwd($this->conn_id);
  103. }
  104. //功能:刪除文件
  105. //$path:文件路徑
  106. //刪除成功返回true,否則返回false。
  107. function delfile($path = null)
  108. {
  109. if(ftp_delete($this->conn_id, $path))
  110. {
  111. $this->message("刪除文件成功");
  112. return true;
  113. }
  114. else
  115. {
  116. $this->message("刪除文件失敗");
  117. return false;
  118. }
  119. }
  120. //功能:打印信息
  121. //$str:要打印的信息
  122. function message($str = null)
  123. {
  124. if(!emptyempty($str))
  125. {
  126. echo $str;
  127. }
  128. }
  129. //功能:關閉ftp連接
  130. function closeftp()
  131. {
  132. ftp_close($this->conn_id);
  133. }
  134. }
  135. /*
  136. 一下為示范;
  137. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  138. <?php
  139. include 'ftp.php';
  140. $conn=new Net_FTP();
  141. //$ftppath=$conn->redir();
  142. $ftppath='/test';
  143. $locpath="/home/liye/public_html/php_ftp/test";
  144. //$conn->uploadfile($ftppath,$locpath);
  145. //$conn->newdir('test/123/1233');
  146. //$conn->deldir('test/123/1233');
  147. //$conn->delfile($ftppath);
  148. $conn->delfile('ftp.php');
  149. ?>
  150. */
Copyright © Linux教程網 All Rights Reserved