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) 文 ...
随机推荐
- .Net设计模式_建造者模式
引言: 建造者的特点是过程,需要建造对象的过程是一样的,如:软件项目,过程都是,POC.投标.立项.软件过程.收款,那么标准的软件项目都是这个过程,只是不同的项目在做这个过程的内容不一样.所以需要有一 ...
- Android_Broadcast
/** * Broadcast(广播):是一种广泛运用的在应用程序之间传输信息的机制 * * BroadcastReceiver(广播接收者) * 是对发送出来的广播进行过滤接收并响应的一类组件,它就 ...
- PHP 概述 特点 基础语法
PHP是什么 http://php.net/manual/zh/intro-whatis.php#intro-whatis PHP(Hypertext Preprocessor,超文本预处理器)是一 ...
- magento 常用的函数
1.Magento eav_attribute表中source如何指定自定义数据来源 如果你引用的类名为yebihai_usermanage_model_entity_school你必须完整的给出地 ...
- YII中的表单挂件
利用助手(widget)在页面实现表单 控制器中 <?php class YiiFormController extends Controller { public function actio ...
- css position 定位
fixed 属于绝对定位,相对于浏览器窗口定位 (IE 6不支持) relative 相对定位,通过设置垂直或水平位置,让这个元素"相对于"它的原始起点进行移动. ...
- JSON 与List转换类封装
json与list转换小结: import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; impor ...
- 响应式框架中,table表头自动换行的解决办法
最近在用bootstrap开发网站,在处理一张table的时候发现,通过PC端查看样式正常,在手机上查看时,因为屏幕小,表格被压缩的厉害,表头和数据变形如下图 后来网上找了一下,发现一个好用的CSS属 ...
- 淘宝开源Web服务器Tengine安装教程
简介Tengine是由淘宝核心系统部基于Nginx开发的Web服务器,它在Nginx的基础上,针对大访问量网站的需求,添加了很多功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,淘宝商 ...
- C++ Dialog Box Command IDs
#define IDOK 1 #define IDCANCEL 2 #define IDABORT 3 #define IDRETRY 4 #define IDIGNORE 5 #define IDY ...