Ftp.class.php 4.99 KB
<?php
dirname(__FILE__).'/config/config.php';
class Util_Ftp
{
	private $host = "localhost";//FTP HOST
	private $port = "21";		//FTP port
	private $user = "Anonymous";//FTP user
	private $password = "Email";	//FTP password
	private $link_id = "";		//FTP hand
	private $is_login = "";		//is login 
	private $debug = 1;
	private $local_dir = "";	//local path for upload or download
	private $rootdir = "";		//FTP root path of FTP server
	private $dir = "/";			//FTP current path

	/**
	 * 初始化函数
	 * 
	 * @param string $user
	 * @param string $password
	 * @param string $host
	 * @param int $port
	 */
	public function __construct($user = FTP_SERVER_USER, $password = FTP_SERVER_PASSWORD,
				$host = FTP_SERVER_HOST, $port = FTP_SERVER_PORT)
	{
		if(strlen($user)) 
		{
			$this->user = $user;
		}
		if(strlen($password))
		{
			$this->password = $password;
		} 
		if(strlen($host))
		{
			$this->host = $host;
		} 
		if(is_numeric($port))
		{
			$this->port = $port;
		}
		$this->login();
		$this->rootdir 	= $this->pwd();
		$this->dir 		= $this->rootdir;
	}
	
	/**
	 * 提示
	 * 
	 * @param string $msg
	 * @param string $line
	 * @return unknown_type
	 */
	public function halt($msg,$line=__LINE__)
	{
		echo "FTP Error in line:$line<br/>\n";
		echo "FTP Error message:$msg<br/>\n";
		exit();
	}
	
	/**
	 * 登录
	 */
	public function login()
  	{
		if(!$this->link_id)
		{
			$this->link_id = ftp_connect($this->host,$this->port) or $this->halt("can not connect to host:$this->host:$this->port",__LINE__);
		}
		if(!$this->is_login)
		{
			//记录时间
			$startTime = time();
			while(1)
			{
				$this->is_login = @ftp_login($this->link_id, $this->user, $this->pass);
				//如果时间超过60秒,则可以有问题
				if((time()-$startTime)>=60)
				{
					echo 'error';
					exit();
				}
				if($this->is_login)
				{
					//echo "Connected as $ftp_user@$ftp_server\n";
					break;
				}
			}

		}
 	}
 	
 	/**
 	 * 返回远程 FTP 服务器的系统类型
 	 * 
 	 * @return int
 	 */
	public function systype()
	{
		return ftp_systype($this->link_id);
	}
	
	/**
	 *  返回当前目录名
	 *  
	 * @return string
	 */
	public function pwd()
	{
		$this->login();
		$dir = ftp_pwd($this->link_id);
		$this->dir = $dir;
		return $dir;
	}
	
	/**
	 * 切换到当前目录的父目录
	 * 
	 * @return boolean
	 */
	public function cdup()
	{
		$this->login();
		$isok =  ftp_cdup($this->link_id);
		if($isok) $this->dir = $this->pwd();
		return $isok;
	}
	
	/**
	 * 切换当前目录
	 * 
	 * @param string$dir
	 * @return boolean
	 */
	public function cd($dir)
	{
		$this->login();
		$isok = ftp_chdir($this->link_id,$dir);
		if($isok) $this->dir = $dir;
		return $isok;
	}
	
	/**
	 * 返回给定目录的文件列表
	 * 
	 * @param string $dir
	 * @return array
	 */
	public function nlist($dir="")
	{
		$this->login();
		if(!$dir) $dir = ".";
		$arr_dir = ftp_nlist($this->link_id,$dir);
		return $arr_dir;
	}
	
	/**
	 * 返回指定目录下文件的详细列表
	 * 
	 * @param string $dir
	 * @return array
	 */
	public function rawlist($dir="/")
	{
		$this->login();
		$arr_dir = ftp_rawlist($this->link_id,$dir);
		return $arr_dir;
	}
	
	/**
	 * 建立新目录
	 * 
	 * @param string $dir
	 * @return boolean
	 */
	public function mkdir($dir)
	{
		$this->login();
		return @ftp_mkdir($this->link_id,$dir);
	}
	
	/**
	 * 返回指定文件的大小
	 * 
	 * @param string $file
	 * @return int
	 */
	public function file_size($file)
	{
		$this->login();
		$size = ftp_size($this->link_id,$file);
		return $size;
	}
	
	/**
	 * 修改文件属性
	 * 
	 * @param string $file
	 * @param int $mode
	 * @return boolean
	 */
	public function chmod($file,$mode=0666)
	{
		$this->login();
		return ftp_chmod($this->link_id,$file,$mode);
	}
	
	/**
	 * 删除文件
	 * 
	 * @param string $remote_file
	 * @return boolean
	 */
	public function delete($remote_file)
	{
		$this->login();
		return @ftp_delete($this->link_id,$remote_file);
	}
	
	/**
	 * 下载
	 * 
	 * @param string $local_file
	 * @param string $remote_file
	 * @param string $mode
	 * @return mix
	 */
	public function get($local_file,$remote_file,$mode=FTP_BINARY)
	{
		$this->login();
		return @ftp_get($this->link_id,$local_file,$remote_file,$mode);
	}
	
	/**
	 * 上传
	 * 
	 * @param $local_file
	 * @param $remote_file
	 * @param $mode
	 * @return boolean
	 */
	public function put($local_file, $remote_file, $mode=FTP_BINARY)
	{
		$this->login();
		return @ftp_put($this->link_id,$remote_file,$local_file,$mode);
	}
	
	/**
	 * 上传数据
	 * 
	 * @param string $remote_file
	 * @param string $data
	 * @param $mode
	 * @return boolean
	 */
	public function put_string($remote_file,$data,$mode=FTP_BINARY)
	{
		$this->login();
		$tmp = "/tmp";//ini_get("session.save_path");
		$tmpfile = tempnam($tmp,"tmp_");
		$fp = @fopen($tmpfile,"w+");
		if($fp)
		{
			fwrite($fp,$data);
			fclose($fp);
		}
		else
		{
			return 0;
		} 
		$isok = $this->put($tmpfile, $remote_file,FTP_BINARY);
		@unlink($tmpfile);
		return $isok;
	}

	/**
	 * 关闭
	 */
	public function close()
	{
		@ftp_quit($this->link_id);
	}

}