<?php
/********************************************
*FTP类
*******************************************/
namespace ftp;
class ftp
{
public $off; // 返回操作状态(成功/失败)
public $conn_id; // FTP连接
public $FTP_HOST = 'xxxxxxx';
public $FTP_PORT = 21;
public $FTP_USER = 'xxxx';
public $FTP_PASS = 'xxxx';
/**
* 方法:FTP连接
* @FTP_HOST -- FTP主机
* @FTP_PORT -- 端口
* @FTP_USER -- 用户名
* @FTP_PASS -- 密码
*/
function __construct()
{
$this->conn_id = @ftp_connect($this->FTP_HOST,$this->FTP_PORT) or die("FTP服务器连接失败");
@ftp_login($this->conn_id,$this->FTP_USER,$this->FTP_PASS) or die("FTP服务器登陆失败");
}
function ftpUplode($fileName,$localPath,$serverPath,$type=true)
{
if($type) $this->dir_mkdirs($serverPath);
$this->off = @ftp_put($this->conn_id,$serverPath.$fileName,$localPath,FTP_BINARY);
if(!$this->off) echo "文件上传失败,请检查权限及路径是否正确!";
}
/**
* 方法:删除文件
* @path -- 路径
*/
function del_file($serverPath)
{
$this->off = @ftp_delete($this->conn_id,$serverPath);
if(!$this->off) echo "文件删除失败,请检查权限及路径是否正确!";
}
/**
* 方法:生成目录
* @path -- 路径
*/
function dir_mkdirs($path)
{
$path_arr = explode('/',$path); // 取目录数组
$file_name = array_pop($path_arr); // 弹出文件名
$path_div = count($path_arr); // 取层数
foreach($path_arr as $val) // 创建目录
{
if(@ftp_chdir($this->conn_id,$val) == FALSE)
{
$tmp = @ftp_mkdir($this->conn_id,$val);
if($tmp == FALSE)
{
echo "目录创建失败,请检查权限及路径是否正确!";
exit;
}
@ftp_chdir($this->conn_id,$val);
}
}
for($i=1;$i<=$path_div;$i++) // 回退到根
{
@ftp_cdup($this->conn_id);
}
}
}