php的ftp类
1.需求
了解php的ftp使用
2.例子
使用CI封装好的ftp类库
上传
$this->load->library('ftp'); $config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE; $this->ftp->connect($config);
//路径要绝对路径
$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775); $this->ftp->close();
下载文件列表
$this->load->library('ftp'); $config['hostname'] = 'ftp.example.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE; $this->ftp->connect($config); $list = $this->ftp->list_files('/public_html/'); print_r($list); $this->ftp->close();
3.自己写的class
<?php
class ftp{
public $hostname = '';
public $username = '';
public $password = '';
public $port = 21;
public $passive = TRUE;
public $debug = FALSE;
protected $conn_id;
public function __construct($config =array())
{
if(count($config)>0)
{
$this->initialize($config);
}
}
public function initialize($config)
{
foreach ($config as $key => $val)
{
if (isset($this->$key))
{
$this->$key = $val;
}
}
$this->hostname = preg_replace('|.+?://|', '', $this->hostname);
}
public function connect($config = array())
{
if (count($config) > 0)
{
$this->initialize($config);
}
if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
{
if ($this->debug === TRUE)
{
echo "ftp_unable_to_connect";
exit();
}
return FALSE;
}
if ( ! $this->_login())
{
if ($this->debug === TRUE)
{
echo "ftp_unable_to_login";
exit();
}
return FALSE;
}
if ($this->passive === TRUE)
{
ftp_pasv($this->conn_id, TRUE);
}
return TRUE;
}
protected function _login()
{
return @ftp_login($this->conn_id, $this->username, $this->password);
}
protected function _is_conn()
{
if ( ! is_resource($this->conn_id))
{
if ($this->debug === TRUE)
{
echo 'ftp_no_connection';
exit();
}
return FALSE;
}
return TRUE;
}
public function changedir($path, $suppress_debug = FALSE)
{
if ( ! $this->_is_conn())
{
return FALSE;
}
$result = @ftp_chdir($this->conn_id, $path);
if ($result === FALSE)
{
if ($this->debug === TRUE && $suppress_debug === FALSE)
{
echo 'ftp_unable_to_changedir';
exit();
}
return FALSE;
}
return TRUE;
}
public function mkdir($path, $permissions = NULL)
{
if ($path === '' OR ! $this->_is_conn())
{
return FALSE;
}
$result = @ftp_mkdir($this->conn_id, $path);
if ($result === FALSE)
{
if ($this->debug === TRUE)
{
echo 'ftp_unable_to_mkdir';
exit();
}
return FALSE;
}
if ($permissions !== NULL)
{
$this->chmod($path, (int) $permissions);
}
return TRUE;
}
public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
{
if ( ! $this->_is_conn())
{
return FALSE;
}
if ( ! file_exists($locpath))
{
echo 'ftp_no_source_file';
return FALSE;
}
if ($mode === 'auto')
{
// Get the file extension so we can set the upload type
$ext = $this->_getext($locpath);
$mode = $this->_settype($ext);
}
$mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
if ($result === FALSE)
{
if ($this->debug === TRUE)
{
echo 'ftp_unable_to_upload';
exit();
}
return FALSE;
}
if ($permissions !== NULL)
{
$this->chmod($rempath, (int) $permissions);
}
return TRUE;
}
public function download($rempath, $locpath, $mode = 'auto')
{
if ( ! $this->_is_conn())
{
return FALSE;
}
if ($mode === 'auto')
{
// Get the file extension so we can set the upload type
$ext = $this->_getext($rempath);
$mode = $this->_settype($ext);
}
$mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);
if ($result === FALSE)
{
if ($this->debug === TRUE)
{
echo 'ftp_unable_to_download';
exit();
}
return FALSE;
}
return TRUE;
}
public function delete_file($filepath)
{
if ( ! $this->_is_conn())
{
return FALSE;
}
$result = @ftp_delete($this->conn_id, $filepath);
if ($result === FALSE)
{
if ($this->debug === TRUE)
{
echo 'ftp_unable_to_delete';
exit();
}
return FALSE;
}
return TRUE;
}
public function chmod($path, $perm)
{
if ( ! $this->_is_conn())
{
return FALSE;
}
if (@ftp_chmod($this->conn_id, $perm, $path) === FALSE)
{
if ($this->debug === TRUE)
{
echo 'ftp_unable_to_chmod';
exit();
}
return FALSE;
}
return TRUE;
}
protected function _getext($filename)
{
return (($dot = strrpos($filename, '.')) === FALSE)
? 'txt'
: substr($filename, $dot + 1);
}
protected function _settype($ext)
{
return in_array($ext, array('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'), TRUE)
? 'ascii'
: 'binary';
}
public function close()
{
return $this->_is_conn()
? @ftp_close($this->conn_id)
: FALSE;
}
}
$obj = new ftp();
$config['hostname'] = 'http://xia.com';
$config['username'] = 'web';
$config['password'] = 'web';
$config['debug'] = TRUE;
$obj->connect($config);
//路径要绝对路径
$obj->upload("C:\\Users\\Administrator\\Desktop\\e.txt", '/www/3.txt', 'ascii', 0775);
$obj->close();
参考资料:
http://codeigniter.org.cn/user_guide/libraries/ftp.html
http://php.net/manual/zh/book.ftp.php
php的ftp类的更多相关文章
- 【转】深入PHP FTP类的详解
FTP是一种文件传输协议,它支持两种模式,一种方式叫做Standard (也就是Active,主动方式),一种是 Passive (也就是PASV,被动方式). Standard模式 FTP 的客户端 ...
- PHP操作FTP类 (上传下载移动创建等)
使用PHP操作FTP-用法 Php代码 收藏代码 <? // 联接FTP服务器 $conn = ftp_connect(ftp.server.com); // 使用username和passwo ...
- 一个封装比较完整的FTP类——clsFTP
前几天,看见园子里面的博友写了一个支持断点续传的FTP类,一时技痒,干脆写了个更完整的clsFtp类.只是我写这个clsFtp不是支持断点续传的目的,而是为了封装FTP几个基本常用的操作接口. 功能 ...
- Ftp类
using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO ...
- C# 实现 微软WebRequestMethods.Ftp类中的FTP操作功能
先奉献一个测试地址,ftp内的文件请勿删除,谢谢 FtpEntity fe = "); 由于代码量较多,只抽出一部分,详细代码请移步 ftp://wjshan0808.3vhost.net ...
- 封装FTP类
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...
- 关于FTP操作的功能类
自己在用的FTP类,实现了检查FTP链接以及返回FTP没有反应的情况. public delegate void ShowError(string content, string title); // ...
- ftp中ftpClient类的API
org.apache.commons.NET.ftp Class FTPClient类FTPClient java.lang.Object java.lang.Object继承 org.apache ...
- FTP上传文件提示550错误原因分析。
今天测试FTP上传文件功能,同样的代码从自己的Demo移到正式的代码中,不能实现功能,并报 Stream rs = ftp.GetRequestStream()提示远程服务器返回错误: (550) 文 ...
随机推荐
- C、Shell、Perl基于Tomcat开发CGI程序环境配置
基于Tomcat7.0版本号配置CGI开发环境,步聚例如以下: 以我的Tomcat7安装文件夹为例:TOMCA_HOME = /Users/yangxin/Documents/devToos/java ...
- 【转】GitHub平台最火Android开源项目整理——2013-08-25 17
http://game.dapps.net/news/developer/9199.html GitHub在中国的火爆程度无需多言,越来越多的开源项目迁移到GitHub平台上.更何况,基于不要重复造轮 ...
- 第一个html程序
<html><head><title> 表单</title> </head><body><form action=&quo ...
- kali2.0如何安装中文输入法
由于kali的更新源是国外网站,替换成国内的镜像站,具体操作如下: 打开终端输入 leafpad /etc/apt/sources.list 把下面的源粘贴进去,原有内容注释掉 #中科大源deb ...
- day-1
/* 倒计时就要结束了 在机房的最后一个晚上 恩 就要结束了 上午考试 下午背板 找了几个原题敲了敲 晚上zjk老妈送的饭 撑死死死死了 好吃23333 吃饭完和zjk在机房门口楼梯上聊了一会 恩 以 ...
- 一个月的时间--java从一无所有到能用框架做点东西出来
四月20号到六月2号 因为顺利完成了Struts在线考试系统的学习,基本掌握了struts框架的原理和他众多复杂的标签.趁着下一件事情还没到时间,也顾不上写昨天研习的student部分和今天stude ...
- asterisk webrtc使用SIPML5初体验
一直尝试,web呼叫xlite终端没有,主要是配置问题: 其中sip.conf配置如下: [general] context=public ; Default context for incoming ...
- mysql常见问题
Q:ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.111' (61)A: 找到my.cnf,把#bind-addres ...
- ASP.NET Identity 用户注册相关设定
此部分可以在 Web项目中的App_Start目录下的 IdentityConfig.cs 文件进行设置. 1.配置密码的验证逻辑 manager.PasswordValidator = new Pa ...
- c语言学习之基础知识点介绍(十五):函数的指针
一.函数的指针的介绍 /* 函数指针: 函数的指针,本质上一个指针 指向函数的指针,就是一个函数指针. 回忆:我们写的源代码编译成二进制的指令集,一串交给CPU执行的指令 先存在内存里面,然后CPU读 ...