<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<?php
/*
php ftp类主要功能:
1.连接ftp并登陆;
2.创建目录和删除目录;
3.上传文件和删除文件;
*/
include 'config.php';
class Net_FTP {
var $ftp_server;
var $ftp_user;
var $ftp_pass;
var $ftp_port;
var $conn_id;
function Net_FTP() {
$this->ftp_server = server;
$this->ftp_user = username;
$this->ftp_pass = password;
$this->ftp_port = port;
// 建立连接
$this->conn_id = ftp_connect($this->ftp_server, $this->ftp_port) or die("不能够连接到 $this->ftp_server");
// 尝试登陆
if (!ftp_login($this->conn_id, $this->ftp_user, $this->ftp_pass))
{
$this->message("连接失败 $this->ftp_user");
}
else
{
$this->message("连接成功 $this->ftp_user ");
}
}
//功能:创建新的目录
//$path默认为空目录
//创建成功返回true,否则返回false。
function newdir($path = null)
{
if($this->ftp_is_dir($this->conn_id,$path)||@ftp_mkdir($this->conn_id,$path))
return true;
if(!$this->newdir(dirname($path)))
return false;
ftp_mkdir($this->conn_id,$path);
return true;
}
//验证是否为目录
//对$path进行验证:如果是目录返回true,否则返回false。
function ftp_is_dir($path)
{
$original_directory = ftp_pwd($this->conn_id);
if(@ftp_chdir($this->conn_id,$path))
{
ftp_chdir($this->conn_id,$original_directory);
return true;
}
else
return false;
}
//功能:上传文件
//$ftppath:存在ftp服务器位置;$localpath:本地文件位置;
//上传成功返回true,否则返回false。
function uploadfile($ftppath = null, $localpath = null)
{
if(!empty($ftppath) && !empty($localpath))
{
$ret = ftp_nb_put($this->conn_id, $ftppath, $localpath, FTP_BINARY);
while ($ret == FTP_MOREDATA)
{
$ret = ftp_nb_continue ($this->conn_id);
}
if ($ret != FTP_FINISHED)
{
$this->message( "上传失败");
return false;
}
else
{
$this->message("上传成功");
return true;
}
}
}
//功能:删除目录
//$dir:要删除的目录
//删除成功返回true,否则返回false。
function deldir($dir = null)
{
if (ftp_rmdir($this->conn_id, $dir))
{
$this->message("删除目录成功");
return true;
}
else
{
$this->message("删除目录失败");
return false;
}
}
//功能:返回目录
//返回当前目录名称
function redir()
{
return ftp_pwd($this->conn_id);
}
//功能:删除文件
//$path:文件路径
//删除成功返回true,否则返回false。
function delfile($path = null)
{
if(ftp_delete($this->conn_id, $path))
{
$this->message("删除文件成功");
return true;
}
else
{
$this->message("删除文件失败");
return false;
}
}
//功能:打印信息
//$str:要打印的信息
function message($str = null)
{
if(!empty($str))
{
echo $str;
}
}
//功能:关闭ftp连接
function closeftp()
{
ftp_close($this->conn_id);
}
}
/*
一下为示范;
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<?php
include 'ftp.php';
$conn=new Net_FTP();
//$ftppath=$conn->redir();
$ftppath='/test';
$locpath="/home/liye/public_html/php_ftp/test";
//$conn->uploadfile($ftppath,$locpath);
//$conn->newdir('test/123/1233');
//$conn->deldir('test/123/1233');
//$conn->delfile($ftppath);
$conn->delfile('ftp.php');
?>
*/
php ftp类
最新推荐文章于 2021-03-10 01:16:09 发布