Location: PHPKode > scripts > FTP best script > ftp.php
<?php
/**
 * Trieda ftp slúži pre prácu s ftp. Download, upload (aj rekuzívny), listing...
 *
 * @author Kristián Feldsam, hide@address.com
 */
class ftp {
	private $conn, $login_result, $logData;
	
	function __construct($ftpHost,$ftpUser,$ftpPass,$retry=3,$ftpPasv=0){
		ini_set('memory_limit', '256M');
		$this->ftpUser = $ftpUser;
		$this->ftpPass = $ftpPass;
		$this->ftpHost = $ftpHost;
		$this->retry = $retry;
		$this->ftpPasv = $ftpPasv;
		$this->initConn();
	}
	
	function initConn(){
		$this->conn = ftp_connect($this->ftpHost);
		$this->login_result = ftp_login($this->conn, $this->ftpUser, $this->ftpPass);
		if($this->conn && $this->login_result){
			ftp_pasv($this->conn, $this->ftpPasv);
			return true;
		}
		return false;
	}
	
	public function put($destinationFile, $sourceFile, $retry=0){
		if(file_exists($sourceFile)){
			if(!$this->isDir($sourceFile,1)){
				$this->createSubDirs($destinationFile);
				if(!ftp_put($this->conn, $destinationFile, $sourceFile, FTP_BINARY)){
					$retry++;
					if($retry > $this->retry){
						$this->logData('Error when uploading file: '.$sourceFile.' => '.$destinationFile,'error');
						return false;
					}
					echo 'Retry: '.$retry."\n";
					$this->reconnect();
					$this->put($destinationFile, $sourceFile, $retry);
				}else{
					$this->logData($sourceFile.' => '.$destinationFile,'ok');
					return true;
				}
			}else{
				$this->recursive($destinationFile, $sourceFile, 'put');
			}
		}
	}
	
	public function get($destinationFile, $sourceFile, $retry=0){
		if(!$this->isDir($sourceFile,0)){
			echo $sourceFile.' => '.$destinationFile."\n";
			$this->createSubDirs($destinationFile,0,1);
			if(!ftp_get($this->conn, $destinationFile, $sourceFile, FTP_ASCII)){
				$retry++;
				if($retry > $this->retry){
					$this->logData('Error when downloading file: '.$sourceFile.' => '.$destinationFile,'error');
					return false;
				}
				echo 'Retry: '.$retry."\n";
				$this->reconnect();
				$this->get($destinationFile, $sourceFile, $retry);
			}else{
				$this->logData($sourceFile.' => '.$destinationFile,'ok');
				return true;
			}
		}else{
			$this->recursive($destinationFile, $sourceFile, 'get');
		}
	}
	
	public function makeDir($dir,$local=0){
		if($local){
			if(!file_exists($dir) && !is_dir($dir))return mkdir($dir, 0777); else return true;
		}else{
			ftp_mkdir($this->conn,$dir);
			return ftp_chmod($this->conn, 0777, $dir);
		}
	}
	
	public function cdUp($local){
		return $local ? chdir('..') : ftp_cdup($this->conn);
	}
	
	public function listFiles($file,$local=0){
		if($local){
			return @scandir($file);
		}else{
			if(!ereg('/',$file)){
				return ftp_nlist($this->conn, $file);
			}else{
				$dirs = explode('/',$file);
				foreach($dirs as $dir){
					$this->changeDir($dir,$local);
				}
				$last = count($dirs)-1;
				$this->cdUp($local);
				$list = ftp_nlist($this->conn, $dirs[$last]);
				$i=0;
				foreach($dirs as $dir){
					if($i<$last)$this->cdUp($local);
					$i++;
				}
				return $list;
			}
		}
	}
	
	public function pwd($local=0){
		return $local ? getcwd() : ftp_pwd($this->conn);
	}
	
    public function changeDir($dir,$local=0){
    	return $local ? chdir($dir) : @ftp_chdir($this->conn, $dir);
    }
	
	function createSubDirs($file,$last=0,$local=0,$chDirBack=1){
		if(ereg('/',$file)){
			$origin = $this->pwd($local);
			if(!$last)$file = substr($file, 0, strrpos($file,'/'));
			$dirs = explode('/',$file);
			foreach($dirs as $dir){
				if(!$this->isDir($dir,$local)){
					$this->makeDir($dir,$local);
					$this->changeDir($dir,$local);
				}else{
					$this->changeDir($dir,$local);
				}
			}
			if($chDirBack)$this->changeDir($origin,$local);
		}
	}
	
	function recursive($destinationFile, $sourceFile, $mode){
		if($mode == 'put')$local = 1; else $local = 0;
		$list = $this->listFiles($sourceFile,$local);
		echo "\n".'Folder: '.$sourceFile."\n";
		print_r($list);
		$x=0;
		$z=0;
		foreach($list as $file){
			if($file == '.' || $file == '..')continue;
			$destFile = $destinationFile.'/'.$file;
			$srcFile = $sourceFile.'/'.$file;
			if($this->isDir($srcFile,$local)){
				$this->recursive($destFile, $srcFile, $mode);
			}else{
				
				if($local){
					$this->put($destFile, $srcFile);
				}else{
					$this->get($destFile, $srcFile);				}
			} 
		}
	}
	
	public function isDir($dir,$local){
		if($local)return is_dir($dir);
		if($this->changeDir($dir))return $this->cdUp(0);
		return false;
	}
	
	function logData($data,$what){
		$this->logData[$what][] = $data;
	}
	
	public function getLogData(){
		return $this->logData;
	}
	
	public function reconnect(){
		$this->closeConn();
		$this->initConn();
	}
	
	public function closeConn(){
		return ftp_close($this->conn);
	}
}
?>
Return current item: FTP best script