ftp.init.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.2 国产PHP开发框架 扩展类库-FTP类
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class ftpInit {
  12. private $linkid;
  13. private $timeout = 50;
  14. /**
  15. * FTP-ftp链接
  16. * @param string $string 字符串
  17. * @return bool
  18. */
  19. public function connect(array $config) {
  20. $port = (isset($config['port'])) ? (int) $config['port'] : 21; //端口号
  21. $this->linkid = ftp_connect($config['service'], $port);
  22. if (!$this->linkid) return false;
  23. @ftp_set_option($this->linkid, FTP_TIMEOUT_SEC, $this->timeout);
  24. if (@!ftp_login($this->linkid, $config['username'], $config['password'])) {
  25. return false;
  26. }
  27. return true;
  28. }
  29. /**
  30. * FTP-文件上传
  31. * @param string $local_file 本地文件
  32. * @param string $ftp_file Ftp文件
  33. * @return bool
  34. */
  35. public function upload($local_file, $ftp_file) {
  36. if (empty($local_file) || empty($ftp_file)) return false;
  37. $ftppath = dirname($ftp_file);
  38. if (!empty($ftppath)) { //创建目录
  39. $this->make_dir($ftppath);
  40. @ftp_chdir($this->linkid, $ftppath);
  41. $ftp_file = basename($ftp_file);
  42. }
  43. $ret = ftp_nb_put($this->linkid, $ftp_file, $local_file, FTP_BINARY);
  44. while ($ret == FTP_MOREDATA) {
  45. $ret = ftp_nb_continue($this->linkid);
  46. }
  47. if ($ret != FTP_FINISHED) return false;
  48. return true;
  49. }
  50. /**
  51. * FTP-文件下载
  52. * @param string $local_file 本地文件
  53. * @param string $ftp_file Ftp文件
  54. * @return bool
  55. */
  56. public function download($local_file, $ftp_file) {
  57. if (empty($local_file) || empty($ftp_file)) return false;
  58. $ret = ftp_nb_get($this->linkid, $local_file, $ftp_file, FTP_BINARY);
  59. while ($ret == FTP_MOREDATA) {
  60. $ret = ftp_nb_continue ($this->linkid);
  61. }
  62. if ($ret != FTP_FINISHED) return false;
  63. return true;
  64. }
  65. /**
  66. * FTP-创建目录
  67. * @param string $path 路径地址
  68. * @return bool
  69. */
  70. public function make_dir($path) {
  71. if (empty($path)) return false;
  72. $dir = explode("/", $path);
  73. $path = ftp_pwd($this->linkid) . '/';
  74. $ret = true;
  75. for ($i=0; $i<count($dir); $i++) {
  76. $path = $path . $dir[$i] . '/';
  77. if (!@ftp_chdir($this->linkid, $path)) {
  78. if (!@ftp_mkdir($this->linkid, $dir[$i])) {
  79. $ret = false;
  80. break;
  81. }
  82. }
  83. @ftp_chdir($this->linkid, $path);
  84. }
  85. if (!$ret) return false;
  86. return true;
  87. }
  88. /**
  89. * FTP-删除文件目录
  90. * @param string $dir 删除文件目录
  91. * @return bool
  92. */
  93. public function del_dir($dir) {
  94. $dir = $this->checkpath($dir);
  95. if (@!ftp_rmdir($this->linkid, $dir)) {
  96. $this->close();
  97. return false;
  98. }
  99. $this->close();
  100. return true;
  101. }
  102. /**
  103. * FTP-删除文件
  104. * @param string $file 删除文件
  105. * @return bool
  106. */
  107. public function del_file($file) {
  108. $file = $this->checkpath($file);
  109. if (@!ftp_delete($this->linkid, $file)) {
  110. $this->close();
  111. return false;
  112. }
  113. $this->close();
  114. return true;
  115. }
  116. /**
  117. * FTP-FTP上的文件列表
  118. * @param string $path 路径
  119. * @return bool
  120. */
  121. public function nlist($path = '/') {
  122. return ftp_nlist($this->linkid, $path);
  123. }
  124. /**
  125. * FTP-改变文件权限值
  126. * @param string $file 文件
  127. * @param string $val 值
  128. * @return bool
  129. */
  130. public function ftp_chmod($file, $val = 0777) {
  131. return @ftp_chmod($this->linkid, $val, $file);
  132. }
  133. /**
  134. * FTP-返回文件大小
  135. * @param string $file 文件
  136. * @return bool
  137. */
  138. public function file_size($file) {
  139. return ftp_size($this->linkid, $file);
  140. }
  141. /**
  142. * FTP-文件修改时间
  143. * @param string $file 文件
  144. * @return bool
  145. */
  146. public function mdtm($file) {
  147. return ftp_mdtm($this->linkid, $file);
  148. }
  149. /**
  150. * FTP-更改ftp上的文件名称
  151. * @param string $oldname 旧文件
  152. * @param string $newname 新文件名称
  153. * @return bool
  154. */
  155. public function changename($oldname, $newname) {
  156. return ftp_rename ($this->linkid, $oldname, $newname);
  157. }
  158. /**
  159. * FTP-关闭链接
  160. * @return bool
  161. */
  162. public function close() {
  163. ftp_close($this->linkid);
  164. }
  165. /**
  166. * FTP-检测path
  167. * @return String
  168. */
  169. private function checkpath($path) {
  170. return (isset($path)) ? trim(str_replace('\\', '/', $path), '/') . '/' : '/';
  171. }
  172. }