1.操作类

<?php
class FtpService
{
protected $connect = 0;
public function __construct()
{
$this->connect = $this->openServer();
if(($this->connect === 0) || ($this->connect === 1)) return $this->connect;
}

/**
* 打开并登录服务器
*
* @return mixed
* 0:服务器连接失败
* 1:服务器登录失败
* resource 连接标识
*/
public function openServer(){
//选择服务器
$config = config('ftp.'); //ftp配置

//连接服务器
$connect = ftp_connect($config['host'], $config['port']);
if($connect == false) return false;

//登录服务器
if(!ftp_login($connect, $config['user'], $config['pwd'])){
return false;
}else{
echo "connect success!"; echo "<br>";
}

//打开被动模式,数据的传送由客户机启动,而不是由服务器开始
ftp_pasv($connect, false);

//返回连接标识
return $connect;
}

/**
* 创建目录并将目录定位到当请目录
*
* @param resource $connect 连接标识
* @param string $dirPath 目录路径
* @return mixed
* 2:创建目录失败
* true:创建目录成功
*/
public function makeDir($dirPath){
$dirPath = '/' . trim($dirPath, '/');
$dirPath = explode('/', $dirPath);
foreach ($dirPath as $dir){
if($dir == '') $dir = '/';
//判断目录是否存在

if(!@ftp_chdir($this->connect, $dir)){
//判断目录是否创建成功
if(@ftp_mkDir($this->connect, $dir) == false){
return false;
}
@ftp_chdir($this->connect, $dir);
}
}

//关闭服务器
$this->closeServer($this->connect);
return true;
}

/**
* 关闭服务器
*
* @param resource $connect 连接标识
*/
public function closeServer($connect){
if(!empty($connect)) ftp_close($connect);
}

/**
* 上传文件
*
* @param string $flag 服务器标识
* @param string $local 上传文件的本地路径
* @param string $remote 上传文件的远程路径
* @return int
*/
public function upload($local, $remote){
//上传文件目录处理
$mdr = $this->makeDir(dirname($remote));
if($mdr === false) return false;

if (!file_exists($local)){
return false;
}

$connect = $this->openServer();
//上传文件
$result = ftp_put($connect, $remote, $local, FTP_BINARY);
//关闭服务器
$this->closeServer($connect);

//返回结果
return (!$result) ? false : true;
}

/**
* 删除文件
*
* @param string $flag 服务器标识
* @param string $remote 文件的远程路径
* @return int
*/
public function delete($remote){
//删除
$result = ftp_delete($this->connect, $remote);
//关闭服务器
$this->closeServer($this->connect);

//返回结果
return (!$result) ? false : true;
}

/**
* 读取文件
*
* @param string $flag 服务器标识
* @param string $remote 文件的远程路径
* @return mixed
*/
public function read($remote){
//读取
$result = ftp_nlist($this->connect, $remote);

//关闭服务器
$this->closeServer($this->connect);

echo "<pre>";
print_r($result);exit;
//返回结果
foreach ($result as $key => $value){
if(in_array($value, array('.', '..'))) unset($result[$key]);
}
return array_values($result);
}

/**
* 下载文件
*/
public function down($local,$remote){
$result = ftp_get($this->connect,$local,$remote,FTP_BINARY);
$this->closeServer($this->connect);
return $result;
}
}

2.ftp连接配置:
<?php
return [
'host' => 'IP',
'port' => 21,
'user' => '账号',
'pwd' => '密码'
];
3.ftp操作调用测试: ftp目录结构(IP/homes/,下面是在homes目录下测试)
    public function ftp_test(){
$path = iconv('UTF-8','GB2312',$_SERVER['DOCUMENT_ROOT']."/uploads/photos/test.txt");
$ftp = new FtpService();

// $result = $ftp->delete("homes/test.txt"); //删除操作
// $result = $ftp->read("homes"); //读操作
// $result = $ftp->down("test.txt",'/homes/test.txt'); //下载操作
// $result = $ftp->makeDir('/homes/testdir/'); //创建文件夹

$result = $ftp->upload($path,'/homes/test1.txt'); //上传操作

echo $result;
}
添加ftp扩展:php.ini文件添加:extension=php_ftp.dll



PHP中ftp的连接与操作的更多相关文章

  1. golang中使用gorm连接mysql操作

    一.代码 package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/go- ...

  2. php大力力 [024节]PHP中的字符串连接操作(2015-08-27)

    2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作  阅读:次   时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...

  3. JNDI和在tomcat中配置DBCP连接池 元数据的使用 DBUtils框架的使用 多表操作

    1 JNDI和在tomcat中配置DBCP连接池 JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.namin ...

  4. 数据库和linq中的 join(连接)操作

    sql中的连接 sql中的表连接有inner join,left join(left outer join),right join(right outer join),full join(full o ...

  5. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  6. Android中FTP服务器、客户端搭建以及SwiFTP、ftp4j介绍

    本文主要内容: 1.FTP服务端部署---- 基于Android中SwiFTP开源软件介绍: 2.FTP客户端部署 --- 基于ftp4j开源jar包的客户端开发 : 3.使用步骤 --- 如何测试我 ...

  7. FTP服务器搭建及操作(一)

    FTP服务器搭建及操作(一) FTP搭建 PHP FTP操作 搭建方法参照(windows):http://www.cnblogs.com/lidan/archive/2012/06/04/25351 ...

  8. Linux中ftp的常用命令

    转自:https://www.jb51.net/article/103904.htm FTP命令 ftp> ascii # 设定以ASCII方式传送文件(缺省值) ftp> bell # ...

  9. FTP-Linux中ftp服务器搭建

    一.FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp ...

随机推荐

  1. Apache配置 7.静态元素过期时间

    (1)介绍 那到底能缓存多久呢?如果服务器上的某个图片更改了,那么应该访问新的图片才对.这就涉及一个静态文件缓存时长的问题,也叫作"缓存过期时间".在httpd的配置文件中,我们是 ...

  2. Python基础(1)——变量和数据类型[xiaoshun]

    目录 一.变量 1.概述 Variables are used to store information to be referenced(引用)and manipulated(操作) in a co ...

  3. Asa's Chess Problem

    一.题目 给定一张 \(n\times n\) 的矩阵,每个点上面有黑棋或者是白棋,给定 \(\frac{n\times n}{2}\) 对可以交换的位置,每对位置一定在同一行 \(/\) 同一列.\ ...

  4. 使用nodejs进行了简单的文件分卷工具

    关键词:node fs readline generator (在这之前需要声明的是这篇博客的应用范围应该算是相当狭隘,写出来主要也就是给自己记录一下临时兴起写的一个小工具,仅从功能需求上来说我相信是 ...

  5. Go语言学习 学习资料汇总

    从进入实验室以来,一直听小溪师兄说Go语言,但是第一学期的课很多,一直没有时间学习,现在终于空出来时间学习,按照我的学习习惯,我一般分为三步走 学习一门语言首先要知道学会了能干什么, 然后再把网上的资 ...

  6. POJ_2752 Seek the Name, Seek the Fame 【KMP】

    一.题目 POJ2752 二.分析 比较明显的KMP运用. 但是这题不是只找一个,仔细看题后可以发现相当于是在找到最大的满足条件的后缀后,再在这个后缀里面找满足条件的后缀. 可以不断的运用KMP得出答 ...

  7. API管理工具

    开源的api文档管理系统 api文档 php 在项目中,需要协同开发,所以会写许多API文档给其他同事,以前都是写一个简单的TXT文本或Word文档,口口相传,这种方式比较老土了,所以,需要有个api ...

  8. Python 随笔2-0319

    一 数据类型 1.整型-int 类型  存年龄.工资.成绩等这样的数据类型可以用int类型 2.浮点型-小数类型(float),带小数点的 3.布尔类型-非真即假  只有这二种:True和Flase, ...

  9. 仿String()构造器函数 【总结】

    需求 实现以下方法: 控制台结果: 需求分析: 首先确定new调用的this和什么对象绑定,如果跟默认返回的对象绑定肯定做不到 [ ] 这样的访问,所以要在构造器内部返回一个包装过的数组 1.leng ...

  10. 致命错误:Python.h:没有那个文件或目录

    yum search python3 | grep dev sudo yum install python3xxx-devel