ftps.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * FTP操作类
  4. * @author chenzhouyu
  5. *
  6. *使用$ftps = pc_base::load_sys_class('ftps');进行初始化。
  7. *首先通过 $ftps->connect($host,$username,$password,$post,$pasv,$ssl,$timeout);进行FTP服务器连接。
  8. *通过具体的函数进行FTP的操作。
  9. *$ftps->mkdir() 创建目录,可以创建多级目录以“/abc/def/higk”的形式进行多级目录的创建。
  10. *$ftps->put()上传文件
  11. *$ftps->rmdir()删除目录
  12. *$ftps->f_delete()删除文件
  13. *$ftps->nlist()列出指定目录的文件
  14. *$ftps->chdir()变更当前文件夹
  15. *$ftps->get_error()获取错误信息
  16. */
  17. class ftps {
  18. //FTP 连接资源
  19. private $link;
  20. //FTP连接时间
  21. public $link_time;
  22. //错误代码
  23. private $err_code = 0;
  24. //传送模式{文本模式:FTP_ASCII, 二进制模式:FTP_BINARY}
  25. public $mode = FTP_BINARY;
  26. /**
  27. * 连接FTP服务器
  28. * @param string $host    服务器地址
  29. * @param string $username   用户名
  30. * @param string $password   密码
  31. * @param integer $port     服务器端口,默认值为21
  32. * @param boolean $pasv 是否开启被动模式
  33. * @param boolean $ssl      是否使用SSL连接
  34. * @param integer $timeout 超时时间 
  35. */
  36. public function connect($host, $username = '', $password = '', $port = '21', $pasv = false, $ssl = false, $timeout = 30) {
  37. $start = time();
  38. if ($ssl) {
  39. if (!$this->link = @ftp_ssl_connect($host, $port, $timeout)) {
  40. $this->err_code = 1;
  41. return false;
  42. }
  43. } else {
  44. if (!$this->link = @ftp_connect($host, $port, $timeout)) {
  45. $this->err_code = 1;
  46. return false;
  47. }
  48. }
  49. if (@ftp_login($this->link, $username, $password)) {
  50. if ($pasv) ftp_pasv($this->link, true);
  51. $this->link_time = time()-$start;
  52. return true;
  53. } else {
  54. $this->err_code = 1;
  55. return false;
  56. }
  57. register_shutdown_function(array(&$this,'close'));
  58. }
  59. /**
  60. * 创建文件夹
  61. * @param string $dirname 目录名,
  62. */
  63. public function mkdir($dirname) {
  64. if (!$this->link) {
  65. $this->err_code = 2;
  66. return false;
  67. }
  68. $dirname = $this->ck_dirname($dirname);
  69. $nowdir = '/';
  70. foreach ($dirname as $v) {
  71. if ($v && !$this->chdir($nowdir.$v)) {
  72. if ($nowdir) $this->chdir($nowdir);
  73. @ftp_mkdir($this->link, $v);
  74. }
  75. if($v) $nowdir .= $v.'/';
  76. }
  77. return true;
  78. }
  79. /**
  80. * 上传文件
  81. * @param string $remote 远程存放地址
  82. * @param string $local 本地存放地址
  83. */
  84. public function put($remote, $local) {
  85. if (!$this->link) {
  86. $this->err_code = 2;
  87. return false;
  88. }
  89. $dirname = pathinfo($remote,PATHINFO_DIRNAME);
  90. if (!$this->chdir($dirname)) {
  91. $this->mkdir($dirname);
  92. }
  93. if (@ftp_put($this->link, $remote, $local, $this->mode)) {
  94. return true;
  95. } else {
  96. $this->err_code = 7;
  97. return false;
  98. }
  99. }
  100. /**
  101. * 删除文件夹
  102. * @param string $dirname 目录地址
  103. * @param boolean $enforce 强制删除
  104. */
  105. public function rmdir($dirname, $enforce = false) {
  106. if (!$this->link) {
  107. $this->err_code = 2;
  108. return false;
  109. }
  110. $list = $this->nlist($dirname);
  111. if ($list && $enforce) {
  112. $this->chdir($dirname);
  113. foreach ($list as $v) {
  114. $this->f_delete($v);
  115. }
  116. } elseif ($list && !$enforce) {
  117. $this->err_code = 3;
  118. return false;
  119. }
  120. @ftp_rmdir($this->link, $dirname);
  121. return true;
  122. }
  123. /**
  124. * 删除指定文件
  125. * @param string $filename 文件名
  126. */
  127. public function f_delete($filename) {
  128. if (!$this->link) {
  129. $this->err_code = 2;
  130. return false;
  131. }
  132. if (@ftp_delete($this->link, $filename)) {
  133. return true;
  134. } else {
  135. $this->err_code = 4;
  136. return false;
  137. }
  138. }
  139. /**
  140. * 返回给定目录的文件列表
  141. * @param string $dirname 目录地址
  142. * @return array 文件列表数据
  143. */
  144. public function nlist($dirname) {
  145. if (!$this->link) {
  146. $this->err_code = 2;
  147. return false;
  148. }
  149. if ($list = @ftp_nlist($this->link, $dirname)) {
  150. return $list;
  151. } else {
  152. $this->err_code = 5;
  153. return false;
  154. }
  155. }
  156. /**
  157. * 在 FTP 服务器上改变当前目录
  158. * @param string $dirname 修改服务器上当前目录
  159. */
  160. public function chdir($dirname) {
  161. if (!$this->link) {
  162. $this->err_code = 2;
  163. return false;
  164. }
  165. if (@ftp_chdir($this->link, $dirname)) {
  166. return true;
  167. } else {
  168. $this->err_code = 6;
  169. return false;
  170. }
  171. }
  172. /**
  173. * 获取错误信息
  174. */
  175. public function get_error() {
  176. if (!$this->err_code) return false;
  177. $err_msg = array(
  178. '1'=>'Server can not connect',
  179. '2'=>'Not connect to server',
  180. '3'=>'Can not delete non-empty folder',
  181. '4'=>'Can not delete file',
  182. '5'=>'Can not get file list',
  183. '6'=>'Can not change the current directory on the server',
  184. '7'=>'Can not upload files'
  185. );
  186. return $err_msg[$this->err_code];
  187. }
  188. /**
  189. * 检测目录名
  190. * @param string $url 目录
  191. * @return 由 / 分开的返回数组
  192. */
  193. private function ck_dirname($url) {
  194. $url = str_replace('\\', '/', $url);
  195. $urls = explode('/', $url);
  196. return $urls;
  197. }
  198. /**
  199. * 关闭FTP连接
  200. */
  201. public function close() {
  202. return @ftp_close($this->link);
  203. }
  204. }