发现官方类库没有FTP库,就随手写了一个,还不完善。<?php <br />
class FTPAction extends CommonAction{<br>
public function index(){<br>
import('ORG.Util.Ftp');<br>
set_time_limit(0);<br>
$ftp=new FTP("222.2.2.200",21,"anonymous");<br>
foreach ($list as $v){<br>
if (!$ftp->isDir($v))<br>
$result.= "文件:".pathinfo(iconv('GBK', 'UTF-8', $v),PATHINFO_BASENAME)."<br>";<br>
else <br>
$result.= "目录:<a>".pathinfo(iconv('GBK', 'UTF-8', $v),PATHINFO_BASENAME)."</a><br>";<br>
} <br>
$this->assign('result',$result);<br>
$this->display();<br>
<br>
}<br>
}
模板调用{$result}即可。<?php <br />
/**<br>
* FTP - 操作FTP文件类.<br>
*<br>
* @author TaoTao<br>
* @copyright Copyright (c) 2013 TaoTao<br>
* @license New BSD License<br>
* @conn http://blog.kisscn.com/<br>
* @version 1.0<br>
*/<br>
class FTP {<br>
private $host;<br>
private $port=21;<br>
private $user;<br>
private $pwd;<br>
private $conn;<br>
private $timeout;<br>
private $ssl=false;<br>
//传送模式{文本模式:FTP_ASCII, 二进制模式:FTP_BINARY}<br>
public $mode = FTP_BINARY;<br>
public function __construct($host,$port=21,$user,$pwd,$timeout=60,$mode="FTP_BINARY",$ssl=false){<br>
$this->host=$host;<br>
$this->port=$port;<br>
$this->user=$user;<br>
$this->pwd=$pwd;<br>
$this->mode=$mode;<br>
$this->timeout=$timeout;<br>
$this->ssl=$ssl;<br>
if($ssl){<br>
$this->conn=ftp_ssl_connect($this->host,$this->port,$this->timeout) or die("FTP连接失败!");<br>
}else{<br>
$this->conn=ftp_connect($this->host,$this->port,$this->timeout) or die("FTP连接失败!");<br>
}<br>
ftp_login($this->conn, $user, $pwd) or die("无法打开FTP连接"); <br>
}<br>
<br>
/**<br>
* 返回给定目录的文件列表<br>
* @param string $dirname 目录地址<br>
* @return array 文件列表数据<br>
*/<br>
public function nlist($dirname) {<br>
if ($list = @ftp_nlist($this->conn, $dirname)) {<br>
return $list;<br>
}<br>
}<br>
/**<br>
* 返回上级目录<br>
* @return boolean<br>
*/<br>
function back_dir()<br>
{<br>
return ftp_cdup($this->conn);<br>
}<br>
/**<br>
* 取得指定目录下文件的详细列表信息<br>
* @param $dirname 目录名称<br>
* @return ArrayObject<br>
*/<br>
function get_file_info($dirname)<br>
{<br>
$list = @ftp_rawlist($this->conn,$dirname);<br>
if(!$list) return false;<br>
$array = array();<br>
foreach($list as $l)<br>
{<br>
$l = preg_replace("/^.*[ ]([^ ]+)$/", "\\1", $l);<br>
if($l == '.' || $l == '..') continue;<br>
$array[] = $l;<br>
}<br>
return $array;<br>
} <br>
/**<br>
* 创建文件夹<br>
* @param string $dirname 目录名,<br>
*/<br>
public function mkdir($dirname) {<br>
$dirname = $this->checkDir($dirname);<br>
$nowdir = '/';<br>
foreach ($dirname as $v) {<br>
if ($v && !$this->cd($nowdir . $v)) {<br>
if ($nowdir)<br>
$this->cd($nowdir);<br>
@ftp_mkdir($this->conn, $v);<br>
}<br>
if ($v)<br>
$nowdir .= $v . '/';<br>
}<br>
return true;<br>
}<br>
/**<br>
* 文件和目录重命名<br>
* @param $old_name 原名称<br>
* @param $new_name 新名称<br>
* @return boolean<br>
*/<br>
function rename($old_name,$new_name)<br>
{<br>
return ftp_rename($this->conn,$old_name,$new_name);<br>
} <br>
/**<br>
* 上传文件<br>
* @param string $remote 远程存放地址<br>
* @param string $local 本地存放地址<br>
*/<br>
public function put($remote, $local) {<br>
<br>
$dirname = pathinfo($remote, PATHINFO_DIRNAME);<br>
if (!$this->cd($dirname)) {<br>
$this->mkdir($dirname);<br>
}<br>
if (@ftp_put($this->conn, $remote, $local, $this->mode)) {<br>
return true;<br>
}<br>
}<br>
/**<br>
* 获取文件的最后修改时间<br>
* @return string $time 返回时间<br>
*/<br>
public function lastUpdatetime($file){<br>
return ftp_mdtm($this->conn,$file);<br>
}<br>
<br>
/**<br>
* 删除指定文件<br>
* @param string $filename 文件名<br>
*/<br>
public function delete($filename) {<br>
if (@ftp_delete($this->conn, $filename)) {<br>
return true;<br>
} <br>
}<br>
<br>
/**<br>
* 在 FTP 服务器上改变当前目录<br>
* @param string $dirname 修改服务器上当前目录<br>
*/<br>
public function cd($dirname) {<br>
if (@ftp_chdir($this->conn, $dirname)) {<br>
return true;<br>
}<br>
} <br>
/**<br>
* 在 FTP 服务器上返回当前目录<br>
* @return string $dirname 返回当前目录名称<br>
*/<br>
public function getPwd() {<br>
return ftp_pwd($this->conn);<br>
<br>
} <br>
/**<br>
* 检测目录名<br>
* @param string $url 目录<br>
* @return 由 / 分开的返回数组<br>
*/<br>
private function checkDir($url) {<br>
$url = str_replace('', '/', $url);<br>
$urls = explode('/', $url);<br>
return $urls;<br>
}<br>
/**<br>
* 检测是否为目录<br>
* @param string $dir 路径<br>
* @return boolean true为目录false为文件<br>
*/ <br>
public function isDir($dir) {<br>
if ($this->cd($dir)){<br>
return true;<br>
}else{<br>
return false;<br>
}<br>
}<br>
/**<br>
* 关闭FTP连接<br>
*/<br>
<br>
public function close() {<br>
return @ftp_close($this->conn);<br>
}<br>
}
AD:真正免费,域名+虚机+企业邮箱=0元