搜索
首页后端开发php教程仿CodeIgniter的FTP类的实现代码

为大家介绍一个仿写CodeIgniter的FTP类,功能很强大,有需要的朋友,可以参考下。

1、文件上传类 ftp.php

<?php
/**
 * FTP基本操作:
 * 1) 登陆;    connect
 * 2) 当前目录文件列表;  filelist
 * 3) 目录改变;   chgdir
 * 4) 重命名/移动;  rename
 * 5) 创建文件夹;  mkdir
 * 6) 删除;    delete_dir/delete_file
 * 7) 上传;    upload
 * 8) 下载    download
 *
 * @author quanshuidingdang
 */
class Ftp {
 private $hostname = '';
 private $username = '';
 private $password = '';
 private $port   = 21;
 private $passive  = TRUE;
 private $debug  = TRUE;
 private $conn_id  = FALSE;
 
 /**
  * 构造函数
  *
  * @param array 配置数组 : $config = array('hostname'=>'','username'=>'','password'=>'','port'=>''...);
  */
 public function __construct($config = array()) {
  if(count($config) > 0) {
   $this->_init($config);
  }
 }
 
 /**
  * FTP连接
  *
  * @access  public
  * @param  array  配置数组
  * @return boolean
  */
 public function connect($config = array()) {
  if(count($config) > 0) {
   $this->_init($config);
  }
  
  if(FALSE === ($this->conn_id = @ftp_connect($this->hostname,$this->port))) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_connect");
   }
   return FALSE;
  }
  
  if( ! $this->_login()) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_login");
   }
   return FALSE;
  }
  
  if($this->passive === TRUE) {
   ftp_pasv($this->conn_id, TRUE);
  }
  
  return TRUE;
 }

 
 /**
  * 目录改变
  *
  * @access  public
  * @param  string  目录标识(ftp)
  * @param boolean 
  * @return boolean
  */
 public function chgdir($path = '', $supress_debug = FALSE) {
  if($path == '' OR ! $this->_isconn()) {
   return FALSE;
  }
  
  $result = @ftp_chdir($this->conn_id, $path);
  
  if($result === FALSE) {
   if($this->debug === TRUE AND $supress_debug == FALSE) {
    $this->_error("ftp_unable_to_chgdir:dir[".$path."]");
   }
   return FALSE;
  }
  
  return TRUE;
 }
 
 /**
  * 目录生成
  *
  * @access  public
  * @param  string  目录标识(ftp)
  * @param int   文件权限列表 
  * @return boolean
  */
 public function mkdir($path = '', $permissions = NULL) {
  if($path == '' OR ! $this->_isconn()) {
   return FALSE;
  }
  
  $result = @ftp_mkdir($this->conn_id, $path);
  
  if($result === FALSE) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_mkdir:dir[".$path."]");
   }
   return FALSE;
  }
  
  if( ! is_null($permissions)) {
   $this->chmod($path,(int)$permissions);
  }
  
  return TRUE;
 }
 
 /**
  * 上传
  *
  * @access  public
  * @param  string  本地目录标识
  * @param string 远程目录标识(ftp)
  * @param string 上传模式 auto || ascii
  * @param int  上传后的文件权限列表 
  * @return boolean
  */
 public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
  if( ! $this->_isconn()) {
   return FALSE;
  }
  
  if( ! file_exists($localpath)) {
   if($this->debug === TRUE) {
    $this->_error("ftp_no_source_file:".$localpath);
   }
   return FALSE;
  }
  
  if($mode == 'auto') {
   $ext = $this->_getext($localpath);
   $mode = $this->_settype($ext);
  }
  
  $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  
  $result = @ftp_put($this->conn_id, $remotepath, $localpath, $mode);
  
  if($result === FALSE) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_upload:localpath[".$localpath."]/remotepath[".$remotepath."]");
   }
   return FALSE;
  }
  
  if( ! is_null($permissions)) {
   $this->chmod($remotepath,(int)$permissions);
  }
  
  return TRUE;
 }
 
 /**
  * 下载
  *
  * @access  public
  * @param  string  远程目录标识(ftp)
  * @param string 本地目录标识
  * @param string 下载模式 auto || ascii 
  * @return boolean
  */
 public function download($remotepath, $localpath, $mode = 'auto') {
  if( ! $this->_isconn()) {
   return FALSE;
  }
  
  if($mode == 'auto') {
   $ext = $this->_getext($remotepath);
   $mode = $this->_settype($ext);
  }
  
  $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  
  $result = @ftp_get($this->conn_id, $localpath, $remotepath, $mode);
  
  if($result === FALSE) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_download:localpath[".$localpath."]-remotepath[".$remotepath."]");
   }
   return FALSE;
  }
  
  return TRUE;
 }
 
 /**
  * 重命名/移动
  *
  * @access  public
  * @param  string  远程目录标识(ftp)
  * @param string 新目录标识
  * @param boolean 判断是重命名(FALSE)还是移动(TRUE) 
  * @return boolean
  */
 public function rename($oldname, $newname, $move = FALSE) {
  if( ! $this->_isconn()) {
   return FALSE;
  }
  
  $result = @ftp_rename($this->conn_id, $oldname, $newname);
  
  if($result === FALSE) {
   if($this->debug === TRUE) {
    $msg = ($move == FALSE) ? "ftp_unable_to_rename" : "ftp_unable_to_move";
    $this->_error($msg);
   }
   return FALSE;
  }
  
  return TRUE;
 }
 
 /**
  * 删除文件
  *
  * @access  public
  * @param  string  文件标识(ftp)
  * @return boolean
  */
 public function delete_file($file) {
  if( ! $this->_isconn()) {
   return FALSE;
  }
  
  $result = @ftp_delete($this->conn_id, $file);
  
  if($result === FALSE) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_delete_file:file[".$file."]");
   }
   return FALSE;
  }
  
  return TRUE;
 }
 
 /**
  * 删除文件夹
  *
  * @access  public
  * @param  string  目录标识(ftp)
  * @return boolean
  */
 public function delete_dir($path) {
  if( ! $this->_isconn()) {
   return FALSE;
  }
  
  //对目录宏的'/'字符添加反斜杠'\'
  $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
 
  //获取目录文件列表
  $filelist = $this->filelist($path);
  
  if($filelist !== FALSE AND count($filelist) > 0) {
   foreach($filelist as $item) {
    //如果我们无法删除,那么就可能是一个文件夹
    //所以我们递归调用delete_dir()
    if( ! @delete_file($item)) {
     $this->delete_dir($item);
    }
   }
  }
  
  //删除文件夹(空文件夹)
  $result = @ftp_rmdir($this->conn_id, $path);
  
  if($result === FALSE) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_delete_dir:dir[".$path."]");
   }
   return FALSE;
  }
  
  return TRUE;
 }
 
 /**
  * 修改文件权限
  *
  * @access  public
  * @param  string  目录标识(ftp)
  * @return boolean
  */
 public function chmod($path, $perm) {
  if( ! $this->_isconn()) {
   return FALSE;
  }
  
  //只有在PHP5中才定义了修改权限的函数(ftp)
  if( ! function_exists('ftp_chmod')) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_chmod(function)");
   }
   return FALSE;
  }
  
  $result = @ftp_chmod($this->conn_id, $perm, $path);
  
  if($result === FALSE) {
   if($this->debug === TRUE) {
    $this->_error("ftp_unable_to_chmod:path[".$path."]-chmod[".$perm."]");
   }
   return FALSE;
  }
  return TRUE;
 }
 
 /**
  * 获取目录文件列表
  *
  * @access  public
  * @param  string  目录标识(ftp)
  * @return array
  */
 public function filelist($path = '.') {
  if( ! $this->_isconn()) {
   return FALSE;
  }
  
  return ftp_nlist($this->conn_id, $path);
 }
 
 /**
  * 关闭FTP
  *
  * @access  public
  * @return boolean
  */
 public function close() {
  if( ! $this->_isconn()) {
   return FALSE;
  }
  
  return @ftp_close($this->conn_id);
 }
 
 /**
  * FTP成员变量初始化
  *
  * @access private
  * @param array 配置数组  
  * @return void
  */
 private function _init($config = array()) {
  foreach($config as $key => $val) {
   if(isset($this->$key)) {
    $this->$key = $val;
   }
  }

  //特殊字符过滤
  $this->hostname = preg_replace('|.+?://|','',$this->hostname);
 }
 
 /**
  * FTP登陆
  *
  * @access  private
  * @return boolean
  */
 private function _login() {
  return @ftp_login($this->conn_id, $this->username, $this->password);
 }
 
 /**
  * 判断con_id
  *
  * @access  private
  * @return boolean
  */
 private function _isconn() {
  if( ! is_resource($this->conn_id)) {
   if($this->debug === TRUE) {
    $this->_error("ftp_no_connection");
   }
   return FALSE;
  }
  return TRUE;
 }
 
 /**
  * 从文件名中获取后缀扩展
  *
  * @access  private
  * @param  string  目录标识
  * @return string
  */
 private function _getext($filename) {
  if(FALSE === strpos($filename, '.')) {
   return 'txt';
  }
  
  $extarr = explode('.', $filename);
  return end($extarr);
 }
 
 /**
  * 从后缀扩展定义FTP传输模式  ascii 或 binary
  *
  * @access  private
  * @param  string  后缀扩展
  * @return string
  */
 private function _settype($ext) {
  $text_type = array (
       'txt',
       'text',
       'php',
       'phps',
       'php4',
       'js',
       'css',
       'htm',
       'html',
       'phtml',
       'shtml',
       'log',
       'xml'
       );
  
  return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
 }
 
 /**
  * 错误日志记录
  *
  * @access  prvate
  * @return boolean
  */
 private function _error($msg) {
  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);
 }
}

/*End of file ftp.php*/
/*Location /Apache Group/htdocs/ftp.php*/
?>

2、调用示例

<?php
require_once('ftp.php');

$config = array(
   'hostname' => 'localhost',
   'username' => 'root',
   'password' => 'root',
   'port' => 21
    );
$ftp = new Ftp();

$ftp->connect($config);
$ftp->upload('ftp_err.log','ftp_upload.log');
$ftp->download('ftp_upload.log','ftp_download.log');

/*from http://bbs.it-home.org*/
/*End of file ftp_demo.php*/
/*Location: /htdocs/ftp_demo.php*/
?>


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?Apr 17, 2025 am 12:24 AM

PHP中使用clone关键字创建对象副本,并通过\_\_clone魔法方法定制克隆行为。1.使用clone关键字进行浅拷贝,克隆对象的属性但不克隆对象属性内的对象。2.通过\_\_clone方法可以深拷贝嵌套对象,避免浅拷贝问题。3.注意避免克隆中的循环引用和性能问题,优化克隆操作以提高效率。

PHP与Python:用例和应用程序PHP与Python:用例和应用程序Apr 17, 2025 am 12:23 AM

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

描述不同的HTTP缓存标头(例如,Cache-Control,ETAG,最后修饰)。描述不同的HTTP缓存标头(例如,Cache-Control,ETAG,最后修饰)。Apr 17, 2025 am 12:22 AM

HTTP缓存头的关键玩家包括Cache-Control、ETag和Last-Modified。1.Cache-Control用于控制缓存策略,示例:Cache-Control:max-age=3600,public。2.ETag通过唯一标识符验证资源变化,示例:ETag:"686897696a7c876b7e"。3.Last-Modified指示资源最后修改时间,示例:Last-Modified:Wed,21Oct201507:28:00GMT。

说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1?说明PHP中的安全密码散列(例如,password_hash,password_verify)。为什么不使用MD5或SHA1?Apr 17, 2025 am 12:06 AM

在PHP中,应使用password_hash和password_verify函数实现安全的密码哈希处理,不应使用MD5或SHA1。1)password_hash生成包含盐值的哈希,增强安全性。2)password_verify验证密码,通过比较哈希值确保安全。3)MD5和SHA1易受攻击且缺乏盐值,不适合现代密码安全。

PHP:服务器端脚本语言的简介PHP:服务器端脚本语言的简介Apr 16, 2025 am 12:18 AM

PHP是一种服务器端脚本语言,用于动态网页开发和服务器端应用程序。1.PHP是一种解释型语言,无需编译,适合快速开发。2.PHP代码嵌入HTML中,易于网页开发。3.PHP处理服务器端逻辑,生成HTML输出,支持用户交互和数据处理。4.PHP可与数据库交互,处理表单提交,执行服务器端任务。

PHP和网络:探索其长期影响PHP和网络:探索其长期影响Apr 16, 2025 am 12:17 AM

PHP在过去几十年中塑造了网络,并将继续在Web开发中扮演重要角色。1)PHP起源于1994年,因其易用性和与MySQL的无缝集成成为开发者首选。2)其核心功能包括生成动态内容和与数据库的集成,使得网站能够实时更新和个性化展示。3)PHP的广泛应用和生态系统推动了其长期影响,但也面临版本更新和安全性挑战。4)近年来的性能改进,如PHP7的发布,使其能与现代语言竞争。5)未来,PHP需应对容器化、微服务等新挑战,但其灵活性和活跃社区使其具备适应能力。

为什么要使用PHP?解释的优点和好处为什么要使用PHP?解释的优点和好处Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版