<?php

class ftpFileHandle{
	var $ftpserver="127.0.0.1";
	var $ftpport=21;
	var $ftpuser="";
	var $ftppass="";
	var $pasvmode=true;
	var $_error=array();
	var $ftpconn=0;
	var $err=false;

	function ftpFileHandle($s=null,$p=null,$u=null,$ps=null){
		$this->ftpconn=0;
		ftpFileHandle::connection($s,$p,$u,$ps);
	}

	function connection($s=null,$p=null,$u=null,$ps=null,$timeout=10){
		$err=false;
		$s!==null AND $this->ftpserver=$s;
		$p!==null AND $this->ftpport=$p;
		$u!==null AND $this->ftpuser=$u;
		$ps!==null AND $this->ftppass=$ps;
		if(empty($this->ftpserver)||empty($this->ftpport)||empty($this->ftpuser)||empty($this->ftppass)){
			$err=true;
			ftpFileHandle::pushError("FTP连接参数丢失");
			$this->ftpconn=0;
			return false;
		}
		if(false!==$this->ftpconn=@ftp_connect($this->ftpserver,$this->ftpport,$timeout)){
			if(@!ftp_login($this->ftpconn,$this->ftpuser,$this->ftppass)){
				$err=true;
				ftpFileHandle::pushError("连登陆失败{$ftpuser}:...失败");
				$this->ftpconn=0;
				return false;
			}
		}else{
			ftpFileHandle::pushError("连接FTP服务器{$this->ftpserver}:{$this->ftpport}失败");
						$this->ftpconn=0;
			return false;
		}
		return $this->ftpconn;
	}

	function fputfile($fp,$remotefile,$ftpmode=FTP_BINARY){
		if(!$this->connectioned())return false;
		$remotefile=str_replace("\\","//",$remotefile);
		while(substr($remotefile,0,1)=="/")$remotefile=substr($remotefile,1,strlen($remotefile));
		if(!$fp){
			$err=true;
			ftpFileHandle::pushError("错误的文件句柄");
			return false;
		}
		$ftpmode!==FTP_BINARY AND $ftpmode!==FTP_ASCII AND $ftpmode=FTP_BINARY;
		if(($result=@ftp_fput($this->ftpconn,$remotefile,$fp,$ftpmode))==false){
			//如果文件名无效，则试图创建他的目录
			if(ftpFileHandle::mkdir(dirname($remotefile))){
				return @ftp_fput($this->ftpconn,$remotefile,$fp,$ftpmode);
			}
			return false;
		}else{
			return true;
		}

	}

	function mkdir($dir){
		if(!$this->connectioned())return false;
		$dir=str_replace("\\","/",$dir);
		$flds=explode("/",$dir);
		for($i=0;$i<count($flds);$i++){
			$fld.=$flds[$i]."/";
			@ftp_mkdir($this->ftpconn,$fld);
		}
		return true;
	}
	
    function deletefile($file){
		if(!$this->connectioned())return false;
		$file=str_replace("\\","/",$file);
		return @ftp_delete($this->ftpconn,$file);
	}
	
	function filelastmodified($file){
		if(!$this->connectioned())return false;
		if(-1!==$lm=ftp_mdtm($this->ftpconn,$file))return $lm;
		return false;
	}
	
	function connectioned(){
		$err=false;
		if(!$this->ftpconn&&!($this->ftpconn=$this->connection())){
			$err=true;
			ftpFileHandle::pushError("连接FTP服务器失败");
			return false;
		}
		return true;
	}
	
	function getpwd(){
		if(!$this->connectioned())return false;
		return ftp_pwd($this->ftpconn);
	}
    
	function chdir($dir){
		if(!$this->connectioned())return false;
		return @ftp_chdir($this->ftpconn,$dir);
	}

	function file_exists($path){
		if(!$this->connectioned())return false;
		return ftp_size($this->ftpconn,$path);
	}

	function isError(){
		return sizeof($this->_error);
	}

	function errorClear(){
		$err=false;
	}
	function getErrorList(){
		return $this->_errors;
	}

	function getLastError(){
		$len = sizeof($this->_errors);
		return $len ? $this->_errors[$len - 1] : false;
	}

	function pushError($errstr){
		$this->_errors[] = $errstr;
	}
}
?>