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) 文 ...
随机推荐
- mysqltuner
http://mysqltuner.com/ MySQLTuner-perl MySQLTuner is a script written in Perl that will assist you w ...
- RHCA442学习笔记-Unit13网络性能调整
UNIT 13 Essential Network Tuning 网络性能调整 目标:1. 应用队列技术最大化网络吞吐量 2. 调整TCP和non-TCP网络soc ...
- 原创 C# 正则表达式 读写 Ini 文件
昨天遇到读ini文件的问题,我知道C#里没有提供相应的类,所有的.net配置都是xml方式存储的. 读取ini文件,很多人直接google一把,然后添加dll引用.介绍的比较详细的,如: C#如何读写 ...
- C# - 集合类 - 集合接口
本篇将介绍关于集合的接口 这些接口定义了所有与集合有关的类的框架 IEnumerable接口 ns:System.Collections 此接口定义了对集合遍历的方法 一般表示元素序列或集合的类都实现 ...
- YII中路径别名
路径别名 system:代表framework目录 system.web:代表framework/web目录 zii:代表framework/zii目录 webroot:代表项目下的app目录(htt ...
- Zepto源码解读
/*******************************************************************************Zepto核心和dom操作******* ...
- Struts,Spring,Hibernate的作用
Spring的作用: 在SSH框假中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因 为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL ...
- jquery基础-包裹 替换 删除 复制
<!doctype html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 深入理解C#中this/partial/null的使用
一.this关键字作用 1.this表示当前运行中的对象 Eg: public class Person { public int age; public string name; public Pe ...
- Linux搭建SSH服务器
Linux 远程登录服务:ssh ·SSH是标准的网络协议,可用于大多数UNIX操作系统,能够实现字符界面的远程登录管理,它默认使用22号端口,采用密文的形式在网络中传输数据,相对于通过明文传输的Te ...