zoukankan      html  css  js  c++  java
  • PHP的FTP类

      1 <?php
    2 /**
    3 * 仿写CodeIgniter的FTP类
    4 * FTP基本操作:
    5 * 1) 登陆; connect
    6 * 2) 当前目录文件列表; filelist
    7 * 3) 目录改变; chgdir
    8 * 4) 重命名/移动; rename
    9 * 5) 创建文件夹; mkdir
    10 * 6) 删除; delete_dir/delete_file
    11 * 7) 上传; upload
    12 * 8) 下载 download
    13 *
    14 * @author quanshuidingdang
    15 */
    16 class Ftp {
    17
    18 private $hostname = '';
    19 private $username = '';
    20 private $password = '';
    21 private $port = 21;
    22 private $passive = TRUE;
    23 private $debug = TRUE;
    24 private $conn_id = FALSE;
    25
    26 /**
    27 * 构造函数
    28 *
    29 * @param array 配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);
    30 */
    31 public function __construct($config = array()) {
    32 if(count($config) > 0) {
    33 $this->_init($config);
    34 }
    35 }
    36
    37 /**
    38 * FTP连接
    39 *
    40 * @access public
    41 * @param array 配置数组
    42 * @return boolean
    43 */
    44 public function connect($config = array()) {
    45 if(count($config) > 0) {
    46 $this->_init($config);
    47 }
    48
    49 if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
    50 if($this->debug === TRUE) {
    51 $this->_error("ftp_unable_to_connect");
    52 }
    53 return FALSE;
    54 }
    55
    56 if( ! $this->_login()) {
    57 if($this->debug === TRUE) {
    58 $this->_error("ftp_unable_to_login");
    59 }
    60 return FALSE;
    61 }
    62
    63 if($this->passive === TRUE) {
    64 ftp_pasv($this->conn_id, TRUE);
    65 }
    66
    67 return TRUE;
    68 }
    69
    70
    71 /**
    72 * 目录改变
    73 *
    74 * @access public
    75 * @param string 目录标识(ftp)
    76 * @param boolean
    77 * @return boolean
    78 */
    79 public function chgdir($path = '', $supress_debug = FALSE) {
    80 if($path == '' OR ! $this->_isconn()) {
    81 return FALSE;
    82 }
    83
    84 $result = @ftp_chdir($this->conn_id, $path);
    85
    86 if($result === FALSE) {
    87 if($this->debug === TRUE AND $supress_debug == FALSE) {
    88 $this->_error("ftp_unable_to_chgdir:dir[".$path."]");
    89 }
    90 return FALSE;
    91 }
    92
    93 return TRUE;
    94 }
    95
    96 /**
    97 * 目录生成
    98 *
    99 * @access public
    100 * @param string 目录标识(ftp)
    101 * @param int 文件权限列表
    102 * @return boolean
    103 */
    104 public function mkdir($path = '', $permissions = NULL) {
    105 if($path == '' OR ! $this->_isconn()) {
    106 return FALSE;
    107 }
    108
    109 $result = @ftp_mkdir($this->conn_id, $path);
    110
    111 if($result === FALSE) {
    112 if($this->debug === TRUE) {
    113 $this->_error("ftp_unable_to_mkdir:dir[".$path."]");
    114 }
    115 return FALSE;
    116 }
    117
    118 if( ! is_null($permissions)) {
    119 $this->chmod($path,(int)$permissions);
    120 }
    121
    122 return TRUE;
    123 }
    124
    125 /**
    126 * 上传
    127 *
    128 * @access public
    129 * @param string 本地目录标识
    130 * @param string 远程目录标识(ftp)
    131 * @param string 上传模式 auto || ascii
    132 * @param int 上传后的文件权限列表
    133 * @return boolean
    134 */
    135 public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
    136 if( ! $this->_isconn()) {
    137 return FALSE;
    138 }
    139
    140 if( ! file_exists($localpath)) {
    141 if($this->debug === TRUE) {
    142 $this->_error("ftp_no_source_file:".$localpath);
    143 }
    144 return FALSE;
    145 }
    146
    147 if($mode == 'auto') {
    148 $ext = $this->_getext($localpath);
    149 $mode = $this->_settype($ext);
    150 }
    151
    152 $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
    153
    154 $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
    155
    156 if($result === FALSE) {
    157 if($this->debug === TRUE) {
    158 $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
    159 }
    160 return FALSE;
    161 }
    162
    163 if( ! is_null($permissions)) {
    164 $this->chmod($remotepath,(int)$permissions);
    165 }
    166
    167 return TRUE;
    168 }
    169
    170 /**
    171 * 下载
    172 *
    173 * @access public
    174 * @param string 远程目录标识(ftp)
    175 * @param string 本地目录标识
    176 * @param string 下载模式 auto || ascii
    177 * @return boolean
    178 */
    179 public function download($remotepath, $localpath, $mode = 'auto') {
    180 if( ! $this->_isconn()) {
    181 return FALSE;
    182 }
    183
    184 if($mode == 'auto') {
    185 $ext = $this->_getext($remotepath);
    186 $mode = $this->_settype($ext);
    187 }
    188
    189 $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
    190
    191 $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
    192
    193 if($result === FALSE) {
    194 if($this->debug === TRUE) {
    195 $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
    196 }
    197 return FALSE;
    198 }
    199
    200 return TRUE;
    201 }
    202
    203 /**
    204 * 重命名/移动
    205 *
    206 * @access public
    207 * @param string 远程目录标识(ftp)
    208 * @param string 新目录标识
    209 * @param boolean 判断是重命名(FALSE)还是移动(TRUE)
    210 * @return boolean
    211 */
    212 public function rename($oldname, $newname, $move = FALSE) {
    213 if( ! $this->_isconn()) {
    214 return FALSE;
    215 }
    216
    217 $result = @ftp_rename($this->conn_id, $oldname, $newname);
    218
    219 if($result === FALSE) {
    220 if($this->debug === TRUE) {
    221 $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
    222 $this->_error($msg);
    223 }
    224 return FALSE;
    225 }
    226
    227 return TRUE;
    228 }
    229
    230 /**
    231 * 删除文件
    232 *
    233 * @access public
    234 * @param string 文件标识(ftp)
    235 * @return boolean
    236 */
    237 public function delete_file($file) {
    238 if( ! $this->_isconn()) {
    239 return FALSE;
    240 }
    241
    242 $result = @ftp_delete($this->conn_id, $file);
    243
    244 if($result === FALSE) {
    245 if($this->debug === TRUE) {
    246 $this->_error("ftp_unable_to_delete_file:file[".$file."]");
    247 }
    248 return FALSE;
    249 }
    250
    251 return TRUE;
    252 }
    253
    254 /**
    255 * 删除文件夹
    256 *
    257 * @access public
    258 * @param string 目录标识(ftp)
    259 * @return boolean
    260 */
    261 public function delete_dir($path) {
    262 if( ! $this->_isconn()) {
    263 return FALSE;
    264 }
    265
    266 //对目录宏的'/'字符添加反斜杠'\'
    267 $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
    268
    269 //获取目录文件列表
    270 $filelist = $this->filelist($path);
    271
    272 if($filelist !== FALSE AND count($filelist) > 0) {
    273 foreach($filelist as $item) {
    274 //如果我们无法删除,那么就可能是一个文件夹
    275 //所以我们递归调用delete_dir()
    276 if( ! @delete_file($item)) {
    277 $this->delete_dir($item);
    278 }
    279 }
    280 }
    281
    282 //删除文件夹(空文件夹)
    283 $result = @ftp_rmdir($this->conn_id, $path);
    284
    285 if($result === FALSE) {
    286 if($this->debug === TRUE) {
    287 $this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
    288 }
    289 return FALSE;
    290 }
    291
    292 return TRUE;
    293 }
    294
    295 /**
    296 * 修改文件权限
    297 *
    298 * @access public
    299 * @param string 目录标识(ftp)
    300 * @return boolean
    301 */
    302 public function chmod($path, $perm) {
    303 if( ! $this->_isconn()) {
    304 return FALSE;
    305 }
    306
    307 //只有在PHP5中才定义了修改权限的函数(ftp)
    308 if( ! function_exists('ftp_chmod')) {
    309 if($this->debug === TRUE) {
    310 $this->_error("ftp_unable_to_chmod(function)");
    311 }
    312 return FALSE;
    313 }
    314
    315 $result = @ftp_chmod($this->conn_id, $perm, $path);
    316
    317 if($result === FALSE) {
    318 if($this->debug === TRUE) {
    319 $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");
    320 }
    321 return FALSE;
    322 }
    323 return TRUE;
    324 }
    325
    326 /**
    327 * 获取目录文件列表
    328 *
    329 * @access public
    330 * @param string 目录标识(ftp)
    331 * @return array
    332 */
    333 public function filelist($path = '.') {
    334 if( ! $this->_isconn()) {
    335 return FALSE;
    336 }
    337
    338 return ftp_nlist($this->conn_id, $path);
    339 }
    340
    341 /**
    342 * 关闭FTP
    343 *
    344 * @access public
    345 * @return boolean
    346 */
    347 public function close() {
    348 if( ! $this->_isconn()) {
    349 return FALSE;
    350 }
    351
    352 return @ftp_close($this->conn_id);
    353 }
    354
    355 /**
    356 * FTP成员变量初始化
    357 *
    358 * @access private
    359 * @param array 配置数组
    360 * @return void
    361 */
    362 private function _init($config = array()) {
    363 foreach($config as $key => $val) {
    364 if(isset($this->$key)) {
    365 $this->$key = $val;
    366 }
    367 }
    368
    369 //特殊字符过滤
    370 $this->hostname = preg_replace('|.+?://|','',$this->hostname);
    371 }
    372
    373 /**
    374 * FTP登陆
    375 *
    376 * @access private
    377 * @return boolean
    378 */
    379 private function _login() {
    380 return @ftp_login($this->conn_id, $this->username, $this->password);
    381 }
    382
    383 /**
    384 * 判断con_id
    385 *
    386 * @access private
    387 * @return boolean
    388 */
    389 private function _isconn() {
    390 if( ! is_resource($this->conn_id)) {
    391 if($this->debug === TRUE) {
    392 $this->_error("ftp_no_connection");
    393 }
    394 return FALSE;
    395 }
    396 return TRUE;
    397 }
    398
    399 /**
    400 * 从文件名中获取后缀扩展
    401 *
    402 * @access private
    403 * @param string 目录标识
    404 * @return string
    405 */
    406 private function _getext($filename) {
    407 if(FALSE === strpos($filename, '.')) {
    408 return 'txt';
    409 }
    410
    411 $extarr = explode('.', $filename);
    412 return end($extarr);
    413 }
    414
    415 /**
    416 * 从后缀扩展定义FTP传输模式 ascii 或 binary
    417 *
    418 * @access private
    419 * @param string 后缀扩展
    420 * @return string
    421 */
    422 private function _settype($ext) {
    423 $text_type = array (
    424 'txt',
    425 'text',
    426 'php',
    427 'phps',
    428 'php4',
    429 'js',
    430 'css',
    431 'htm',
    432 'html',
    433 'phtml',
    434 'shtml',
    435 'log',
    436 'xml'
    437 );
    438
    439 return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
    440 }
    441
    442 /**
    443 * 错误日志记录
    444 *
    445 * @access prvate
    446 * @return boolean
    447 */
    448 private function _error($msg) {
    449 return @file_put_contents('ftp_err.log', "date[".date("Y-m-d H:i:s")."]-hostname[".$this->hostname."]-username[".$this->username."]-password[".$this->password."]-msg[".$msg."]\n", FILE_APPEND);
    450 }
    451 }

    以上是类文件,下面是文件操作:

     1 <?php
    2 require_once('ftp.php');
    3
    4 $config = array(
    5 'hostname' => 'localhost',
    6 'username' => 'root',
    7 'password' => 'root',
    8 'port' => 21
    9 );
    10 $ftp = new Ftp();
    11 $ftp->connect($config);
    12 $ftp->upload('ftp_err.log','ftp_upload.log'); //upload('本地文件路径','上传目录');
    13 $ftp->download('ftp_upload.log','ftp_download.log'); //download('FTP上文件路径','要保存到本地的路径');
    14
    15 ?>


    本地转载地址:http://www.oschina.net/code/snippet_167160_5849

  • 相关阅读:
    安卓基础之读取联系人的姓名和电话
    Android基础之6.0系统以上的权限分配
    Android基础之内容提供者的实现
    android中Post方式发送HTTP请求
    安卓基础之Sqlite数据库最最基础操作
    安卓基础之Get方式发送http请求
    安卓基础之国际化
    安卓基础之主题/样式
    安卓基础之Activity的生命周期
    Kotlin入门(14)继承的那些事儿
  • 原文地址:https://www.cnblogs.com/betx/p/2413351.html
Copyright © 2011-2022 走看看