欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

用于thinkphp的FTP类(初步实现)

程序员文章站 2024-02-16 18:33:04
...
发现官方类库没有FTP库,就随手写了一个,还不完善。
用于thinkphp的FTP类(初步实现) class FTPAction extends CommonAction{
public function index(){
import('ORG.Util.Ftp');
set_time_limit(0);
$ftp=new FTP("222.2.2.200",21,"anonymous");
foreach ($list as $v){
if (!$ftp->isDir($v))
$result.= "文件:".pathinfo(iconv('GBK', 'UTF-8', $v),PATHINFO_BASENAME)."
";
else
$result.= "目录:".pathinfo(iconv('GBK', 'UTF-8', $v),PATHINFO_BASENAME)."
";
}
$this->assign('result',$result);
$this->display();

}
}
模板调用{$result}即可。 /**
* FTP - 操作FTP文件类.
*
* @author TaoTao
* @copyright Copyright (c) 2013 TaoTao
* @license New BSD License
* @conn http://blog.kisscn.com/
* @version 1.0
*/
class FTP {
private $host;
private $port=21;
private $user;
private $pwd;
private $conn;
private $timeout;
private $ssl=false;
//传送模式{文本模式:FTP_ASCII, 二进制模式:FTP_BINARY}
public $mode = FTP_BINARY;
public function __construct($host,$port=21,$user,$pwd,$timeout=60,$mode="FTP_BINARY",$ssl=false){
$this->host=$host;
$this->port=$port;
$this->user=$user;
$this->pwd=$pwd;
$this->mode=$mode;
$this->timeout=$timeout;
$this->ssl=$ssl;
if($ssl){
$this->conn=ftp_ssl_connect($this->host,$this->port,$this->timeout) or die("FTP连接失败!");
}else{
$this->conn=ftp_connect($this->host,$this->port,$this->timeout) or die("FTP连接失败!");
}
ftp_login($this->conn, $user, $pwd) or die("无法打开FTP连接");
}

/**
* 返回给定目录的文件列表
* @param string $dirname 目录地址
* @return array 文件列表数据
*/
public function nlist($dirname) {
if ($list = @ftp_nlist($this->conn, $dirname)) {
return $list;
}
}
/**
* 返回上级目录
* @return boolean
*/
function back_dir()
{
return ftp_cdup($this->conn);
}
/**
* 取得指定目录下文件的详细列表信息
* @param $dirname 目录名称
* @return ArrayObject
*/
function get_file_info($dirname)
{
$list = @ftp_rawlist($this->conn,$dirname);
if(!$list) return false;
$array = array();
foreach($list as $l)
{
$l = preg_replace("/^.*[ ]([^ ]+)$/", "\\1", $l);
if($l == '.' || $l == '..') continue;
$array[] = $l;
}
return $array;
}
/**
* 创建文件夹
* @param string $dirname 目录名,
*/
public function mkdir($dirname) {
$dirname = $this->checkDir($dirname);
$nowdir = '/';
foreach ($dirname as $v) {
if ($v && !$this->cd($nowdir . $v)) {
if ($nowdir)
$this->cd($nowdir);
@ftp_mkdir($this->conn, $v);
}
if ($v)
$nowdir .= $v . '/';
}
return true;
}
/**
* 文件和目录重命名
* @param $old_name 原名称
* @param $new_name 新名称
* @return boolean
*/
function rename($old_name,$new_name)
{
return ftp_rename($this->conn,$old_name,$new_name);
}
/**
* 上传文件
* @param string $remote 远程存放地址
* @param string $local 本地存放地址
*/
public function put($remote, $local) {

$dirname = pathinfo($remote, PATHINFO_DIRNAME);
if (!$this->cd($dirname)) {
$this->mkdir($dirname);
}
if (@ftp_put($this->conn, $remote, $local, $this->mode)) {
return true;
}
}
/**
* 获取文件的最后修改时间
* @return string $time 返回时间
*/
public function lastUpdatetime($file){
return ftp_mdtm($this->conn,$file);
}

/**
* 删除指定文件
* @param string $filename 文件名
*/
public function delete($filename) {
if (@ftp_delete($this->conn, $filename)) {
return true;
}
}

/**
* 在 FTP 服务器上改变当前目录
* @param string $dirname 修改服务器上当前目录
*/
public function cd($dirname) {
if (@ftp_chdir($this->conn, $dirname)) {
return true;
}
}
/**
* 在 FTP 服务器上返回当前目录
* @return string $dirname 返回当前目录名称
*/
public function getPwd() {
return ftp_pwd($this->conn);

}
/**
* 检测目录名
* @param string $url 目录
* @return 由 / 分开的返回数组
*/
private function checkDir($url) {
$url = str_replace('', '/', $url);
$urls = explode('/', $url);
return $urls;
}
/**
* 检测是否为目录
* @param string $dir 路径
* @return boolean true为目录false为文件
*/
public function isDir($dir) {
if ($this->cd($dir)){
return true;
}else{
return false;
}
}
/**
* 关闭FTP连接
*/

public function close() {
return @ftp_close($this->conn);
}
}

AD:真正免费,域名+虚机+企业邮箱=0元