Ftp.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. namespace Think\Upload\Driver;
  12. class Ftp
  13. {
  14. /**
  15. * 上传文件根目录
  16. * @var string
  17. */
  18. private $rootPath;
  19. /**
  20. * 本地上传错误信息
  21. * @var string
  22. */
  23. private $error = ''; //上传错误信息
  24. /**
  25. * FTP连接
  26. * @var resource
  27. */
  28. private $link;
  29. private $config = array(
  30. 'host' => '', //服务器
  31. 'port' => 21, //端口
  32. 'timeout' => 90, //超时时间
  33. 'username' => '', //用户名
  34. 'password' => '', //密码
  35. );
  36. /**
  37. * 构造函数,用于设置上传根路径
  38. * @param array $config FTP配置
  39. */
  40. public function __construct($config)
  41. {
  42. /* 默认FTP配置 */
  43. $this->config = array_merge($this->config, $config);
  44. /* 登录FTP服务器 */
  45. if (!$this->login()) {
  46. E($this->error);
  47. }
  48. }
  49. /**
  50. * 检测上传根目录
  51. * @param string $rootpath 根目录
  52. * @return boolean true-检测通过,false-检测失败
  53. */
  54. public function checkRootPath($rootpath)
  55. {
  56. /* 设置根目录 */
  57. $this->rootPath = ftp_pwd($this->link) . '/' . ltrim($rootpath, '/');
  58. if (!@ftp_chdir($this->link, $this->rootPath)) {
  59. $this->error = '上传根目录不存在!';
  60. return false;
  61. }
  62. return true;
  63. }
  64. /**
  65. * 检测上传目录
  66. * @param string $savepath 上传目录
  67. * @return boolean 检测结果,true-通过,false-失败
  68. */
  69. public function checkSavePath($savepath)
  70. {
  71. /* 检测并创建目录 */
  72. if (!$this->mkdir($savepath)) {
  73. return false;
  74. } else {
  75. //TODO:检测目录是否可写
  76. return true;
  77. }
  78. }
  79. /**
  80. * 保存指定文件
  81. * @param array $file 保存的文件信息
  82. * @param boolean $replace 同名文件是否覆盖
  83. * @return boolean 保存状态,true-成功,false-失败
  84. */
  85. public function save($file, $replace = true)
  86. {
  87. $filename = $this->rootPath . $file['savepath'] . $file['savename'];
  88. /* 不覆盖同名文件 */
  89. // if (!$replace && is_file($filename)) {
  90. // $this->error = '存在同名文件' . $file['savename'];
  91. // return false;
  92. // }
  93. /* 移动文件 */
  94. if (!ftp_put($this->link, $filename, $file['tmp_name'], FTP_BINARY)) {
  95. $this->error = '文件上传保存错误!';
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * 创建目录
  102. * @param string $savepath 要创建的目录
  103. * @return boolean 创建状态,true-成功,false-失败
  104. */
  105. public function mkdir($savepath)
  106. {
  107. $dir = $this->rootPath . $savepath;
  108. if (ftp_chdir($this->link, $dir)) {
  109. return true;
  110. }
  111. if (ftp_mkdir($this->link, $dir)) {
  112. return true;
  113. } elseif ($this->mkdir(dirname($savepath)) && ftp_mkdir($this->link, $dir)) {
  114. return true;
  115. } else {
  116. $this->error = "目录 {$savepath} 创建失败!";
  117. return false;
  118. }
  119. }
  120. /**
  121. * 获取最后一次上传错误信息
  122. * @return string 错误信息
  123. */
  124. public function getError()
  125. {
  126. return $this->error;
  127. }
  128. /**
  129. * 登录到FTP服务器
  130. * @return boolean true-登录成功,false-登录失败
  131. */
  132. private function login()
  133. {
  134. extract($this->config);
  135. $this->link = ftp_connect($host, $port, $timeout);
  136. if ($this->link) {
  137. if (ftp_login($this->link, $username, $password)) {
  138. return true;
  139. } else {
  140. $this->error = "无法登录到FTP服务器:username - {$username}";
  141. }
  142. } else {
  143. $this->error = "无法连接到FTP服务器:{$host}";
  144. }
  145. return false;
  146. }
  147. /**
  148. * 析构方法,用于断开当前FTP连接
  149. */
  150. public function __destruct()
  151. {
  152. ftp_close($this->link);
  153. }
  154. }