PHP中ftp的连接与操作
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的连接与操作的更多相关文章
- golang中使用gorm连接mysql操作
一.代码 package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/go- ...
- php大力力 [024节]PHP中的字符串连接操作(2015-08-27)
2015-08-27 php大力力024.PHP中的字符串连接操作 PHP中的字符串连接操作 阅读:次 时间:2012-03-25 PHP字符串的连接的简单实例 时间:2013-12-30 很多 ...
- JNDI和在tomcat中配置DBCP连接池 元数据的使用 DBUtils框架的使用 多表操作
1 JNDI和在tomcat中配置DBCP连接池 JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.namin ...
- 数据库和linq中的 join(连接)操作
sql中的连接 sql中的表连接有inner join,left join(left outer join),right join(right outer join),full join(full o ...
- 通过cmd完成FTP上传文件操作
一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...
- Android中FTP服务器、客户端搭建以及SwiFTP、ftp4j介绍
本文主要内容: 1.FTP服务端部署---- 基于Android中SwiFTP开源软件介绍: 2.FTP客户端部署 --- 基于ftp4j开源jar包的客户端开发 : 3.使用步骤 --- 如何测试我 ...
- FTP服务器搭建及操作(一)
FTP服务器搭建及操作(一) FTP搭建 PHP FTP操作 搭建方法参照(windows):http://www.cnblogs.com/lidan/archive/2012/06/04/25351 ...
- Linux中ftp的常用命令
转自:https://www.jb51.net/article/103904.htm FTP命令 ftp> ascii # 设定以ASCII方式传送文件(缺省值) ftp> bell # ...
- FTP-Linux中ftp服务器搭建
一.FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp ...
随机推荐
- DenseNet的个人总结
DenseNet这篇论文是在ResNet之后一年发表的,由于ResNet在当时引起了很大的轰动,所以DenseNet也将ResNet作为了主要的对比方法,读起来还是比较容易的,全篇只有两个数学公式,也 ...
- 翻译 - ASP.NET Core 基本知识 - 配置(Configuration)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0 ASP ...
- ClickHouse性能优化?试试物化视图
一.前言 ClickHouse是一个用于联机分析(OLAP)的列式数据库管理系统(DBMS):目前我们使用CH作为实时数仓用于统计分析,在做性能优化的时候使用了 物化视图 这一特性作为优化手段,本文主 ...
- [BFS]P1434 [SHOI2002]滑雪
P1434 [SHOI2002]滑雪 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者 ...
- 动图:删除链表的倒数第 N 个结点
本文主要介绍一道面试中常考链表删除相关的题目,即 leetcode 19. 删除链表的倒数第 N 个结点.采用 双指针 + 动图 的方式进行剖析,供大家参考,希望对大家有所帮组. 19. 删除链表的倒 ...
- OO结课了,狂喜
OO结课了,狂喜 哈哈哈哈哈 哈哈哈 哈哈 哈 第十三次作业 UML类图 简要分析: 本次作业是对UML类图进行解析,给到的接口里面已经有了很多类了,但是自带的类肯定是没有反应这些类的结构的.所以就自 ...
- day-10 xctf-cgpwn2
xctf-cgpwn2 题目传送门:https://adworld.xctf.org.cn/task/answer?type=pwn&number=2&grade=0&id=5 ...
- 12.Quick QML-QML 布局(Row、Column、Grid、Flow和嵌套布局) 、Repeater对象
1.Row布局 Row中的item可以不需要使用anchors布局,就能通过行的形式进行布局. 并且item可以使用Positioner附加属性来访问有关其在Row中的位置及其他信息. 示例如下所示, ...
- 脱壳——UPX脱壳原理(脱壳helloworld)
脱壳--UPX脱壳原理 脱壳步骤 1 找到OEP 2 dump(导出)内存文件 3 修复 1 找到OEP 1 程序运行先从壳代码运行,壳代码执行完之后会跳转到真正的OEP,也就是是说第一步,首先要找到 ...
- Nginx篇
1 基本操作命令 先CD到nginx.exe目录 启动nginx服务 nginx start nginx 优雅停止nginx,有连接时会等连接请求完成再杀死worker进程 nginx -s quit ...