<?php
/**
* Class FtpClient
*/
class FtpClient
{
private $host = '';//远程服务器地址
private $user = '';//ftp用户名
private $pass = '';//ftp密码
private $port = 21;//ftp登录端口
private $error = '';//最后失败时的错误信息
protected $conn;//ftp登录资源 /**
* 可以在实例化类的时候配置数据,也可以在下面的connect方法中配置数据
* Ftp constructor.
* @param array $config
*/
public function __construct(array $config = [])
{
empty($config) OR $this->initialize($config);
} /**
* 初始化数据
* @param array $config 配置文件数组
*/
public function initialize(array $config = [])
{
$this->host = $config['host'];
$this->user = $config['user'];
$this->pass = $config['pass'];
$this->port = isset($config['port']) ?: 21;
} /**
* 连接及登录ftp
* @param array $config 配置文件数组
* @return bool
*/
public function connect(array $config = [])
{
empty($config) OR $this->initialize($config);
if (FALSE == ($this->conn = @ftp_connect($this->host))) {
$this->error = "主机连接失败";
return FALSE;
}
if (!$this->_login()) {
$this->error = "服务器登录失败";
return FALSE;
}
return TRUE;
}
/**
* 上传文件到ftp服务器
* @param string $local_file 本地文件路径
* @param string $remote_file 服务器文件地址
* @param string $mode 上传模式(ascii和binary其中之一)
* @param null $permissions 文件夹权限
* @return bool
*/
public function upload($local_file = '', $remote_file = '', $mode = 'auto', $permissions = NULL)
{
if (!file_exists($local_file)) {
$this->error = "本地文件不存在";
return FALSE;
}
if ($mode == 'auto') {
$ext = $this->_get_ext($local_file);
$mode = $this->_set_type($ext);
}
//创建文件夹
$this->_create_remote_dir($remote_file);
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_put($this->conn, $remote_file, $local_file, $mode);//同步上传
if ($result === FALSE) {
$this->error = "文件上传失败";
return FALSE;
}
return TRUE;
}
/**
* 从ftp服务器下载文件到本地
* @param string $local_file 本地文件地址
* @param string $remote_file 远程文件地址
* @param string $mode 上传模式(ascii和binary其中之一)
* @return bool
*/
public function download($local_file = '', $remote_file = '', $mode = 'auto')
{
if ($mode == 'auto') {
$ext = $this->_get_ext($remote_file);
$mode = $this->_set_type($ext);
}
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_get($this->conn, $local_file, $remote_file, $mode);
if ($result === FALSE) {
return FALSE;
}
return TRUE;
} /**
* 删除ftp服务器端文件
* @param string $remote_file 文件地址
* @return bool
*/
public function delete_file($remote_file = '')
{
$result = @ftp_delete($this->conn, $remote_file);
if ($result === FALSE) {
return FALSE;
}
return TRUE;
} /**
* ftp创建多级目录
* @param string $remote_file 要上传的远程图片地址
* @param null $permissions 修改目录权限
*/
private function _create_remote_dir($remote_file = '', $permissions = NULL)
{
$remote_dir = dirname($remote_file);
$path_arr = explode('/', $remote_dir); // 取目录数组
//$file_name = array_pop($path_arr); // 弹出文件名
$path_div = count($path_arr); // 取层数
foreach ($path_arr as $val) // 创建目录
{
if (@ftp_chdir($this->conn, $val) == FALSE) {
$tmp = @ftp_mkdir($this->conn, $val);//此处创建目录时不用使用绝对路径(不要使用:2018-02-20/ceshi/ceshi2,这种路径),因为下面ftp_chdir已经已经把目录切换成当前目录
if ($tmp == FALSE) {
echo "目录创建失败,请检查权限及路径是否正确!";
exit;
}
if ($permissions !== NULL) {
//修改目录权限
$this->_chmod($val, $permissions);
}
@ftp_chdir($this->conn, $val);
}
} for ($i = 0; $i < $path_div; $i++) // 回退到根,因为上面的目录切换导致当前目录不在根目录
{
@ftp_cdup($this->conn);
}
}
/**
* 递归删除ftp端目录
* @param string $remote_dir ftp目录地址
* @return bool
*/
public function delete_dir($remote_dir = '')
{
$list = $this->list_file($remote_dir);
if (!empty($list)) {
$count = count($list);
for ($i = 0; $i < $count; $i++) {
if (!preg_match('#\.#', $list[$i]) && !@ftp_delete($this->conn, $list[$i])) {
//这是一个目录,递归删除
$this->delete_dir($list[$i]);
} else {
$this->delete_file($list[$i]);
}
}
}
if (@ftp_rmdir($this->conn, $remote_dir) === FALSE) {
return FALSE;
}
return TRUE;
} /**
* 更改 FTP 服务器上的文件或目录名
* @param string $old_file 旧文件/文件夹名
* @param string $new_file 新文件/文件夹名
* @return bool
*/
public function remane($old_file = '', $new_file = '')
{
$result = @ftp_rename($this->conn, $old_file, $new_file);
if ($result === FALSE) {
$this->error = "移动失败";
return FALSE;
}
return TRUE;
} /**
*
* @param string $remote_path
*/
/**
* 列出ftp指定目录
* @param string $remote_path 服务器上的路径
* @return array
*/
public function list_file($remote_path = '')
{
$contents = @ftp_nlist($this->conn, $remote_path);
return $contents;
}
/**
* 获取文件的后缀名
* @param string $local_file 本地文件
* @return bool|string
*/
private function _get_ext($local_file = '')
{
return (($dot = strrpos($local_file, '.')) == FALSE) ? 'txt' : substr($local_file, $dot + 1);
} /**
* 根据文件后缀获取上传编码
* @param string $ext
* @return string
*/
private function _set_type($ext = '')
{
//如果传输的文件是文本文件,可以使用ASCII模式,如果不是文本文件,最好使用BINARY模式传输。
return in_array($ext, ['txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'], TRUE) ? 'ascii' : 'binary';
} /**
* 修改目录权限
* @param $path 目录路径
* @param int $mode 权限值
* @return bool
*/
private function _chmod($path, $mode = 0755)
{
if (FALSE == @ftp_chmod($this->conn, $path, $mode)) {
return FALSE;
}
return TRUE;
} /**
* 登录Ftp服务器
* @return bool
*/
private function _login()
{
return @ftp_login($this->conn, $this->user, $this->pass);
} /**
* 获取上传错误信息
* @return string
*/
public function get_error_msg()
{
return $this->error;
} /**
* 关闭ftp连接
* @return bool
*/
public function close()
{
return $this->conn ? @ftp_close($this->conn_id) : FALSE;
}
}

PHP 之ftp客户端类封装实现的更多相关文章

  1. 静态资源上传至远程ftp服务器,ftp工具类封装

    工具类,是一个单独的工程项目 提取必要信息至ftp.properties配置文件中 ftp_host=192.168.110.128 ftp_port=21 ftp_username=ftpuser ...

  2. FTP+SFTP工具类封装-springmore让开发更简单

    github地址:https://github.com/tangyanbo/springmore FTPUtil 该工具基于org.apache.commons.net.ftp.FTPClient进行 ...

  3. 用edtftpj实现Java FTP客户端工具

    edtftpj是一个java FTP工具包,使用非常方便,感觉比Apache的好用,但Apache更灵活.edtftpj有多种版本,分别是java..net和js版本.对于Java版的有一个免费版本. ...

  4. C# 实现FTP客户端

    本文是利用C# 实现FTP客户端的小例子,主要实现上传,下载,删除等功能,以供学习分享使用. 思路: 通过读取FTP站点的目录信息,列出对应的文件及文件夹. 双击目录,则显示子目录,如果是文件,则点击 ...

  5. FTP操作类

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace HGFTP { ...

  6. [转载]C# FTP操作工具类

    本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...

  7. 【转载】HTTP/FTP客户端开发库:libwww、libcurl、libfetch

    网页抓取和ftp访问是目前很常见的一个应用需要,无论是搜索引擎的爬虫,分析程序,资源获取程序,WebService等等都是需 要的,自己开发抓取库当然是最好了,不过开发需要时间和周期,使用现有的Ope ...

  8. 【python】FTP客户端

    Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,函数列举如下 ftp登陆连接 from ftplib import FTP #加 ...

  9. Python的网络编程[1] -> FTP 协议[2] -> 使用 ftplib 建立 FTP 客户端

    使用 ftplib 建立 FTP 客户端 用于建立FTP Client,与 pyftplib 建立的 Server 进行通信. 快速导航 1. 模块信息 2. 建立 FTP 客户端 1. 模块信息 1 ...

随机推荐

  1. 深度优先遍历DFS

    深度优先遍历,这个跟树中的遍历类似,做深度遍历就是访问一个节点之后,在访问这个节点的子节点,依次下去是一个递归的过程. 具体代码: void DFS(MGraph g ,int i) {     in ...

  2. LeetCode 541. Reverse String II (反转字符串 II)

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  3. YTU 2774: Prepare for CET6

    2774: Prepare for CET6 时间限制: 1 Sec  内存限制: 128 MB 提交: 40  解决: 37 题目描述 Hard to force the CET4&6 is ...

  4. Android连接热点的Socket文件传输

    最近把测试丢过来的种种BUG解决后,终于有时间去研究研究Socket通信,再加上以前做的WiFi连接和热点开启,于是有了现在的这篇博文:创建热点发送文件,让另一台手机连接热点接收文件. 效果图: 两台 ...

  5. Luogu3403跳楼机

    https://zybuluo.com/ysner/note/1099616 题面 给你三个数\(x\),\(y\),\(z\),问你能够凑出多少个[1,\(h\)]之间的数. 解析 处理出\(y\) ...

  6. Core Data的那点事儿~

    一.介绍下Core Data CoreData在早些年iOS开发中使用不多,因为其本身性能略低,以及不使用SQL语句而失去的灵活性,再加上FMDB之类封装SQLite的三方框架很好用,所以一直不受待见 ...

  7. Java注解Annotation的用法 - 自定义Annotation实现

    Java注解又称Java标注,是Java语言5.0版本开始支持加入源代码的特殊语法元数据. Java语言中的类.方法.变量.参数和包等都可以被标注.和Javadoc不同,Java标注可以通过反射获取标 ...

  8. Kubernetes 配置 Taint 和 Toleration(污点和容忍)

    通过污点和容忍让pod运行在特定节点上 参考官网:https://k8smeetup.github.io/docs/concepts/configuration/taint-and-toleratio ...

  9. ASP.NET MVC5 之路由器

    这篇博客介绍的很详细 http://www.cnblogs.com/yaozhenfa/p/asp_net_mvc_route_1.html

  10. 【USACO2002 Feb】奶牛自行车队

    [USACO2002 Feb]奶牛自行车队 Time Limit: 1000 ms Memory Limit: 131072 KBytes Description N 头奶牛组队参加自行车赛.车队在比 ...