ftp.init.php 4.8 KB

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