You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.6 KiB

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