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) 文 ...
随机推荐
- [HTTPS] MAN IN THE MIDDLE (MITM)
If you go a public caffee shop, they have free wifi. How could you make sure your infomration cannot ...
- vim: 自己定义颜色主题(colorscheme)
命令模式下输入 :hi[ghlight] 能够看到当前vim的着色风格 命令模式下输入 :sy[ntax] 能够看到当前语法加亮模式,当文档的文字与列表里的正則表達式匹配时,vim会给文字着色.应用缩 ...
- Linux 汇编语言开发指南
http://www.ibm.com/developerworks/cn/linux/l-assembly/
- careercup-递归和动态规划 9.10
9.10 给你一堆n个箱子,箱子宽w,高h,深d.箱子不能翻转,将箱子堆起来时,下面箱子的宽度.高度和深度必须大于上面的箱子.实现一个方法,搭出最高的一堆箱子,箱堆的高度为每个箱子高度的总和. 解法: ...
- 常用linux命令和配置
find只查看文件和只查看目录 find -type f -name clexec find -type d -name clexec 解压rpm [root@sj_x861 2]# ls e ...
- CCLabelTTF 如何支持换行符和换行
参考自http://www.cocos2d-x.org/wiki/How_does_CCLabelTTF_support_line_breaks_and_wrapping 环境: cocos2d-x ...
- Day07 - Python 网络编程 Socket
1. Python 网络编程 Python 提供了两个级别访问网络服务: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口 ...
- 转:maven报错非法字符:\65279 错误
开发中一个项目很早就报这个错,maven报错非法字符:\65279 错误,今天终于忍无可忍要解决它 :编译java文件的时候,有些java文件报非法字符 \65279错误,在网上找和很多 方法,也试了 ...
- xml、xhtml、html、dhtml的区别
1.XML 可扩展标记语言,标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. 可扩展标记语言可以对文档和数据进行结构化处理,从而能够在部门.客户和供应商之间进行交换,实现动态内 ...
- Win7/Win8.1预订升级Win10失败临时解决方案
很多Win7/Win8.1用户在今天凌晨通过微软官方推送的方式升级Win10,但这一过程中遇到了“安装失败”等问题,导致升级无法进行.鉴于这种情况,很多用户选择进入Windows10预下载安装文件夹打 ...