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. WPF 应用 - 通过 js 缩放 System.Windows.Controls.WebBrowser 的内容

    1. 前提 原本是在大屏上展示系统,系统有个功能是加载第三方的网站,第三方网站按照大屏的分辨率写死了宽高: 现需要改到小屏展示系统,而这个第三方的网站不能随着 WebBrowser 窗口的尺寸调整网站 ...

  2. C# 基础 - 日志捕获二使用 log4net

    引入 log4net.dll 项目->添加->新建项->应用程序配置文件,命名为 log4net.config,并把属性的复制到输出目录设置为 如果较新则复制,后续客户端需要读取在 ...

  3. java网络通信不止UDP,TCP

    预备知识 多线程 实现多线程 线程池 IO流 核心功能就是读和写 扩展功能对什么读写,怎么读写,如何优化读写 网络基础 IP IP规定网络上所有的设备都必须有一个独一无二的IP地址,就好比是邮件上都必 ...

  4. 通过unity Distribution Portal发布华为渠道的游戏

    背景说明 前面几个帖子详细介绍了: Unity Editor安装和Apk打包 手把手教您快速运行Unity提供的华为游戏demo 使用unity完成华为游戏的初始化和华为帐号登录 快速开发Unity游 ...

  5. 翻译:《实用的Python编程》06_01_Iteration_protocol

    目录 | 上一节 (5.2 封装) | 下一节 (6.2 自定义迭代) 6.1 迭代协议 本节将探究迭代的底层过程. 迭代无处不在 许多对象都支持迭代: a = 'hello' for c in a: ...

  6. 攻防世界 reverse pingpong

    pingpong  XCTF 3rd-BCTF-2017 java层代码很简单: 1 package com.geekerchina.pingpongmachine; 2 3 import andro ...

  7. Android 之 SimpleAdapter 学习笔记

    •SimpleAdapter简介 simpleAdapter 的扩展性最好,可以定义各种各样的布局出来: 可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框) ...

  8. idea报错Selected Java version 11 is not supported by SDK (maximum 8)

    解决方案

  9. PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...

  10. LiteOS内核源码分析:任务LOS_Schedule

    摘要:调度,Schedule也称为Dispatch,是操作系统的一个重要模块,它负责选择系统要处理的下一个任务.调度模块需要协调处于就绪状态的任务对资源的竞争,按优先级策略从就绪队列中获取高优先级的任 ...