PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/wind/ftp/WindFtp.php

https://gitlab.com/wuhang2003/phpwind
PHP | 215 lines | 103 code | 17 blank | 95 comment | 17 complexity | 7c7c9aeddab8a83f3a9a0d92df52d98e MD5 | raw file
  1. <?php
  2. Wind::import('WIND:ftp.AbstractWindFtp');
  3. /**
  4. * 使用ftp函数实现ftp相关操作
  5. *
  6. * 使用方法和普通类库一样:
  7. * <code>
  8. * Wind::import('WIND:ftp.WindFtp');
  9. * $ftp = new WindFtp(array('server' => '192.168.1.10', 'port' => '21', ‘user' => 'test', 'pwd' => '123456'));
  10. * print_r($ftp->fileList());
  11. * </code>
  12. *
  13. * @author xiaoxia.xu <xiaoxia.xuxx@aliyun-inc.com>
  14. * @copyright ©2003-2103 phpwind.com
  15. * @license http://www.windframework.com
  16. * @version $Id: WindFtp.php 3904 2013-01-08 07:01:26Z yishuo $
  17. * @package ftp
  18. */
  19. class WindFtp extends AbstractWindFtp {
  20. /**
  21. * 被动模式是否开启默认为true开启
  22. *
  23. * @var boolean
  24. */
  25. private $isPasv = true;
  26. /**
  27. * 构造函数
  28. *
  29. * 通过传入config构造链接对象
  30. *
  31. * @param array $config ftp配置文件
  32. */
  33. public function __construct($config = array()) {
  34. $this->connection($config);
  35. }
  36. /**
  37. * 链接ftp
  38. *
  39. * @param array $config ftp的配置信息:
  40. * <ul>
  41. * <li>server: ftp主机地址</li>
  42. * <li>port: ftp链接端口号,默认为21</li>
  43. * <li>user: ftp链接用户名</li>
  44. * <li>pwd: ftp链接用户密码</li>
  45. * <li>dir: ftp链接后切换的目录,默认为空</li>
  46. * <li>timeout: ftp链接超时时间,默认为10秒</li>
  47. * <li>ispasv: ftp是否采用被动模式,默认为1,如果配置为0则表示不开启被动模式,其他值都将设置为开启被动模式</li>
  48. * </ul>
  49. * @return boolean
  50. */
  51. private function connection($config = array()) {
  52. $this->initConfig($config);
  53. if (false === ($this->conn = ftp_connect($this->server, $this->port, $this->timeout))) {
  54. throw new WindFtpException("[ftp.WindFtp.connection] $this->server:$this->port",
  55. WindFtpException::CONNECT_FAILED);
  56. }
  57. if (false == ftp_login($this->conn, $this->user, $this->pwd)) {
  58. throw new WindFtpException('[ftp.WindFtp.connection] ' . $this->user, WindFtpException::LOGIN_FAILED);
  59. }
  60. if ($this->isPasv) {
  61. ftp_pasv($this->conn, true);
  62. }
  63. $this->initRootPath();
  64. return true;
  65. }
  66. /**
  67. * 获得ftp链接
  68. *
  69. * @return resource
  70. */
  71. private function getFtp() {
  72. if (is_resource($this->conn)) return $this->conn;
  73. $this->connection();
  74. return $this->conn;
  75. }
  76. /* (non-PHPdoc)
  77. * @see AbstractWindFtp::rename()
  78. */
  79. public function rename($oldName, $newName) {
  80. return ftp_rename($this->getFtp(), $oldName, $newName);
  81. }
  82. /* (non-PHPdoc)
  83. * @see AbstractWindFtp::delete()
  84. */
  85. public function delete($filename) {
  86. return ftp_delete($this->getFtp(), $filename);
  87. }
  88. /* (non-PHPdoc)
  89. * @see AbstractWindFtp::upload()
  90. */
  91. public function upload($sourceFile, $desFile, $mode = 'I') {
  92. $mode = $this->getMode($sourceFile, $mode);
  93. if (!in_array(($savedir = dirname($desFile)), array('.', '/'))) {
  94. $this->mkdirs($savedir);
  95. }
  96. $desFile = $this->rootPath . WindSecurity::escapePath($desFile);
  97. $result = ftp_put($this->getFtp(), $desFile, $sourceFile, $mode);
  98. if (false === $result) throw new WindFtpException('[ftp.WindFtp.upload] PUT', WindFtpException::COMMAND_FAILED);
  99. $this->chmod($desFile, 0644);
  100. return $this->size($desFile);
  101. }
  102. /* (non-PHPdoc)
  103. * @see AbstractWindFtp::download()
  104. */
  105. public function download($filename, $localname = '', $mode = 'I') {
  106. $mode = $this->getMode($filename, $mode);
  107. return ftp_get($this->getFtp(), $localname, $filename, $mode);
  108. }
  109. /* (non-PHPdoc)
  110. * @see AbstractWindFtp::fileList()
  111. */
  112. public function fileList($dir = '') {
  113. return ftp_nlist($this->getFtp(), $dir);
  114. }
  115. /* (non-PHPdoc)
  116. * @see AbstractWindFtp::close()
  117. */
  118. public function close() {
  119. is_resource($this->conn) && ftp_close($this->conn);
  120. $this->conn = null;
  121. return true;
  122. }
  123. /* (non-PHPdoc)
  124. * @see AbstractWindFtp::initConfig()
  125. */
  126. public function initConfig($config) {
  127. if (!$config || !is_array($config)) return false;
  128. parent::initConfig($config);
  129. $this->isPasv = (isset($config['ispasv']) && $config['ispasv'] == 0) ? false : true;
  130. }
  131. /* (non-PHPdoc)
  132. * @see AbstractWindFtp::mkdir()
  133. */
  134. public function mkdir($dir, $permissions = 0777) {
  135. $result = ftp_mkdir($this->getFtp(), $dir);
  136. if (!$result) return false;
  137. return $this->chmod($result, $permissions) === false ? false : true;
  138. }
  139. /**
  140. * 给文件赋指定权限
  141. *
  142. * @param string $file 待处理的文件
  143. * @param int $permissions 文件的需要的权限
  144. * @return boolean 设置成功返回true,设置失败返回false
  145. */
  146. private function chmod($file, $permissions = 0777) {
  147. return ftp_chmod($this->getFtp(), $permissions, $file);
  148. }
  149. /* (non-PHPdoc)
  150. * @see AbstractWindFtp::pwd()
  151. */
  152. protected function pwd() {
  153. return ftp_pwd($this->getFtp()) . '/';
  154. }
  155. /* (non-PHPdoc)
  156. * @see AbstractWindFtp::changeDir()
  157. */
  158. public function changeDir($dir) {
  159. if (false === ftp_chdir($this->getFtp(), $dir)) {
  160. throw new WindFtpException('[ftp.WindFtp.changeDir] ' . $dir, WindFtpException::COMMAND_FAILED_CWD);
  161. }
  162. return true;
  163. }
  164. /* (non-PHPdoc)
  165. * @see AbstractWindFtp::size()
  166. */
  167. public function size($file) {
  168. return ftp_size($this->getFtp(), $file);
  169. }
  170. /**
  171. * 根据文件获得文件访问的模式
  172. *
  173. * @param string $filename 文件名
  174. * @param string $mode 模式,二进制还是ASCII上传,I为二进制模式,A为ASCII模式,默认为A模式,如果是auto将会根据文件后缀来设置模式
  175. * @return string 返回模式方式FTP_ASCII或是FTP_BINARY
  176. */
  177. private function getMode($filename, $mode) {
  178. if (strcasecmp($mode, 'auto') == 0) {
  179. $ext = WindFile::getSuffix($filename);
  180. $mode = (in_array(strtolower($ext),
  181. array(
  182. 'txt',
  183. 'text',
  184. 'php',
  185. 'phps',
  186. 'php4',
  187. 'js',
  188. 'css',
  189. 'htm',
  190. 'html',
  191. 'phtml',
  192. 'shtml',
  193. 'log',
  194. 'xml'))) ? 'A' : 'I';
  195. }
  196. return ($mode == 'A') ? FTP_ASCII : FTP_BINARY;
  197. }
  198. }