PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wind/ftp/AbstractWindFtp.php

http://github.com/phpwind/windframework
PHP | 266 lines | 100 code | 22 blank | 144 comment | 15 complexity | d11972022f03f12e3afe774722057ed3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * FTP基类
  4. *
  5. * 定义了FTP类拥有的接口
  6. *
  7. * @author xiaoxia.xu <xiaoxia.xuxx@aliyun-inc.com>
  8. * @copyright ©2003-2103 phpwind.com
  9. * @license http://www.windframework.com
  10. * @version $Id$
  11. * @package ftp
  12. */
  13. abstract class AbstractWindFtp {
  14. /**
  15. * ftp主机地址
  16. *
  17. * @var string
  18. */
  19. protected $server = '';
  20. /**
  21. * ftp链接端口号
  22. *
  23. * @var int
  24. */
  25. protected $port = 21;
  26. /**
  27. * ftp链接的用户名
  28. *
  29. * @var string
  30. */
  31. protected $user = '';
  32. /**
  33. * ftp链接的用户密码
  34. *
  35. * @var string
  36. */
  37. protected $pwd = '';
  38. /**
  39. * ftp链接之后使用的当前路径
  40. *
  41. * @var string
  42. */
  43. protected $dir = '';
  44. /**
  45. * ftp链接的过期时间单位秒
  46. *
  47. * @var int
  48. */
  49. protected $timeout = 10;
  50. /**
  51. * 保存ftp的跟目录路径
  52. *
  53. * @var string
  54. */
  55. protected $rootPath = '';
  56. /**
  57. * ftp链接对象
  58. *
  59. * @var resource
  60. */
  61. protected $conn = null;
  62. /**
  63. * 初始化配置信息
  64. *
  65. * @param array $config ftp的配置信息:
  66. * <ul>
  67. * <li>server: ftp主机地址</li>
  68. * <li>port: ftp链接端口号,默认为21</li>
  69. * <li>user: ftp链接用户名</li>
  70. * <li>pwd: ftp链接用户密码</li>
  71. * <li>dir: ftp链接后切换的目录,默认为空</li>
  72. * <li>timeout: ftp链接超时时间,默认为10秒</li>
  73. * </ul>
  74. * @return boolean 配置成功返回true,配置失败返回false
  75. */
  76. public function initConfig($config) {
  77. if (!$config || !is_array($config)) return false;
  78. isset($config['server']) && $this->server = $config['server'];
  79. isset($config['port']) && $this->port = $config['port'];
  80. isset($config['user']) && $this->user = $config['user'];
  81. isset($config['pwd']) && $this->pwd = $config['pwd'];
  82. isset($config['dir']) && $this->dir = $config['dir'];
  83. isset($config['timeout']) && $this->timeout = $config['timeout'];
  84. return true;
  85. }
  86. /**
  87. * 重命名文件
  88. *
  89. * @param string $oldName 现在的文件名
  90. * @param string $newName 新的文件名
  91. * @return boolean 重命名成功则返回true,失败则返回false
  92. */
  93. abstract public function rename($oldName, $newName);
  94. /**
  95. * 删除文件
  96. *
  97. * @param string $filename 待删除的文件
  98. * @return boolean 删除成功返回true,删除失败返回false
  99. */
  100. abstract public function delete($filename);
  101. /**
  102. * 上传文件
  103. *
  104. * @param string $sourceFile 待上传的文件
  105. * @param string $desFile 文件上传的存放位置
  106. * @param string $mode 上传模式二进制还是ASCII上传,I为二进制模式,A为ASCII模式,默认为A模式
  107. * @return int 返回上传文件的大小
  108. */
  109. abstract public function upload($sourceFile, $desFile, $mode = 'A');
  110. /**
  111. * 下载文件
  112. *
  113. * @param string $localfile 下载文件存放位置
  114. * @param string $remotefile 待下载的文件
  115. * @param string $mode 下载的模式二进制还是ASCII上传,I为二进制模式,A为ASCII模式,默认为A模式
  116. * @return boolean 返回文件下载是否成功
  117. */
  118. abstract public function download($localfile, $remotefile = '', $mode = 'A');
  119. /**
  120. * 列出给定目录的文件列表
  121. *
  122. * @param string $dir 目录,默认为空即为当前目录
  123. * @return array 返回该目录下的文件列表
  124. */
  125. abstract public function fileList($dir = '');
  126. /**
  127. * 关闭ftp链接
  128. *
  129. * @return boolean 返回关闭链接是否成功
  130. */
  131. abstract public function close();
  132. /**
  133. * 创建文件夹
  134. *
  135. * @param string $dir 待创建的文件夹
  136. * @return boolean 创建文件夹成功则返回true,失败则返回false
  137. */
  138. abstract public function mkdir($dir);
  139. /**
  140. * 更改当前目录到指定目录下
  141. *
  142. * @param string $dir 需要设置为当前目录的目录
  143. * @return boolean 设置成功则返回true,失败则返回false
  144. */
  145. abstract public function changeDir($dir);
  146. /**
  147. * 获得文件大小
  148. *
  149. * @param string $file 待获取的文件
  150. * @return int 获取成功返回文件大小
  151. */
  152. abstract public function size($file);
  153. /**
  154. * 获得当前路径
  155. *
  156. * @return string 返回当前路径
  157. */
  158. abstract protected function pwd();
  159. /**
  160. * 级联创建文件夹
  161. *
  162. * @param string $dir 待创建文件夹路径
  163. * @param string $permissions 创建的文件夹的权限
  164. * @return boolean 创建成功返回true创建失败返回false
  165. */
  166. public function mkdirs($dir, $permissions = 0777) {
  167. $dir = explode('/', WindSecurity::escapePath($dir));
  168. $dirs = '';
  169. $result = false;
  170. $count = count($dir);
  171. for ($i = 0; $i < $count; $i++) {
  172. if (strpos($dir[$i], '.') === 0) continue;
  173. $result = $this->mkdir($dir[$i], $permissions);
  174. $this->changeDir($this->rootPath . $dirs . $dir[$i]);
  175. $dirs .= "$dir[$i]/";
  176. }
  177. $this->changeDir($this->rootPath);
  178. return $result;
  179. }
  180. /**
  181. * 检查文件是否存在
  182. *
  183. * @param string $filename 待检查的文件
  184. * @return boolean 文件存在则返回true,失败则返回false
  185. */
  186. public function file_exists($filename) {
  187. $directory = substr($filename, 0, strrpos($filename, '/'));
  188. $filename = str_replace("$directory/", '', $filename);
  189. if ($directory) {
  190. $directory = $this->rootPath . $directory . '/';
  191. } else {
  192. $directory = $this->rootPath;
  193. }
  194. $this->changeDir($directory);
  195. $list = $this->fileList();
  196. $this->changeDir($this->rootPath);
  197. if (!empty($list) && in_array($filename, $list)) return true;
  198. return false;
  199. }
  200. /**
  201. * 重设当前目录为初始化目录信息
  202. */
  203. protected function initRootPath() {
  204. $this->rootPath = $this->pwd();
  205. if ($this->dir) {
  206. $this->rootPath .= trim(str_replace('\\', '/', $this->dir), '/') . '/';
  207. }
  208. $this->changeDir($this->rootPath);
  209. }
  210. /**
  211. * 检查文件类型
  212. *
  213. * @param string $filename 待检查的文件
  214. * @return boolean 如果文件带有.php字串则返回false,否则返回true
  215. * @TODO 文件内容的安全性检查
  216. */
  217. protected function checkFile($filename) {
  218. return (str_replace(array('..', '.php.'), '', $filename) != $filename || preg_match('/\.php$/i', $filename));
  219. }
  220. /**
  221. * 获得文件后缀
  222. *
  223. * @param string $filename 文件
  224. * @return string 返回文件的后缀,如果传入的文件没有后缀将会返回txt
  225. */
  226. protected function getExt($filename) {
  227. if (false === strpos($filename, '.')) return 'txt';
  228. $x = explode('.', $filename);
  229. return strtolower(end($x));
  230. }
  231. /**
  232. * 显示错误信息
  233. *
  234. * @param string $str 错误信息
  235. * @param boolean $close 是否关闭链接,默认为true需要关闭链接
  236. */
  237. protected function showError($str, $close = true) {
  238. $close && $this->close();
  239. exit($str);
  240. }
  241. }