PHPRO.ORG

<?php
/**
 * Simple FTP Class
 * 
 * @package FTP
 * @name FTP
 * @version 1.0
 * @author Kevin Waterson
 * @link phpro.org
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 *
 */
final class FTP {

        
/**
         *
         * Constructor, duh!
         *
         * @access      public
         *
         */
        
public function  __construct$ftp_host=null$ftp_user=null$ftp_pass=null )
        {
                
$this->ftp_host $ftp_host;
                
$this->ftp_user $ftp_user;
                
$this->ftp_pass $ftp_pass;
                
$this->ftp_ssl  $this->ftp_ssl;
                
$this->ftp_port $this->ftp_port;
                
$this->ftp_debug $this->ftp_debug;
                
$this->ftp_timeout $this->ftp_timeout;
                
$this->ftp_passive_mode $this->ftp_passive_mode;
        }


        
/**
         * 
         * Setter
         *
         * @access      public
         * @param       string  $name
         *
         */
        
public function __get$name )
        {
                switch( 
$name )
                {
                        case 
'error':
                        case 
'ftp_host':
                        case 
'ftp_user':
                        case 
'ftp_pass':
                        case 
'ftp_path':
                        case 
'ftp_stream':
                        case 
'system_type':
                        return 
$this->$name;
                        break;

                        case 
'ftp_ssl':
                        return 
false;
                        break;

                        case 
'ftp_port':
                        return 
21;
                        break;

                        case 
'ftp_timeout':
                        return 
90;
                        break;

                        case 
'ftp_debug':
                        return 
false;
                        break;

                        case 
'ftp_passive_mode':
                        return 
true;
                        break;

                        default: throw new 
Exception"Unable to get $name);
                }
        }

        
/**
         * 
         * Setter, duh!
         *
         * @access      public
         * @param       string  $name
         * @param       mixed   $value
         *
         */
        
public function __set$name$value )
        {
                switch( 
$name )
                {
                        case 
'error':
                        case 
'ftp_ssl':
                        case 
'ftp_host':
                        case 
'ftp_user':
                        case 
'ftp_pass':
                        case 
'ftp_port':
                        case 
'ftp_path':
                        case 
'ftp_debug':
                        case 
'ftp_stream':
                        case 
'ftp_timeout':
                        case 
'system_type':
                        case 
'ftp_passive_mode':
                        
$this->$name $value;
                        break;

                        default: throw new 
Exception"Unable to set $name);
                }
        }

        
/**
         * Desctructor, close connection
         *
         * @access      public
         *
         */
        
public function  __destruct()
        {
                
$this->close();
        }

        
/**
         * Change currect directory on FTP server
         *
         * @access      public
         * @param       string  $directory
         * @return      bool
         *
         */
        
public function cd$directory=null )
        {
                if( 
ftp_chdir$this->ftp_stream$directory ) )
                {
                        return 
true;
                }
                
$this->error "Failed to change directory to $directory";
                return 
false;
        }

        
/**
         * Set file permissions
         *
         * @access      public
         * @param       int     $permissions 
         * @param       string  $remote_file
         * @return      bool
         *
         */
        
public function chmod$permissions$remote_file=null )
        {
                if( 
ftp_chmod$this->ftp_stream$permissions$remote_file ) )
                {
                        return 
true;
                }
                
$this->error "Failed to set file permissions for \"{$remote_file}\"";
                return 
false;
        }

        
/**
         *
         * Close FTP connection
         *
         * @access      public
         *
         */
        
public function close()
        {
                if( isset( 
$this->ftp_stream ) && $this->ftp_stream != null )
                {
                        
ftp_close$this->ftp_stream );
                        
$this->ftp_stream false;
                }
        }
        
/**
         * Connect to FTP server
         *
         * @access      public
         * @return      bool
         *
         */
        
public function connect()
        {
                
// if this is not an ssl connection
                
if( !$this->ftp_ssl )
                {
                        
$stream = @ftp_connect$this->ftp_host$this->ftp_port$this->ftp_timeout );
                        if( 
$stream !== false )
                        {
                                
$this->ftp_stream $stream;
                                return 
true;
                        }
                        
$this->error "Connection failed to $this->ftp_host";
                        return 
false;
                }
                elseif( 
function_exists"ftp_ssl_connect" ) )
                {
                        
// ssl connection
                        
if( !$this->ftp_stream ftp_ssl_connect$this->ftp_host$this->ftp_port$this->ftp_timeout ) )
                        {
                                
$this->error "SSL connection failed to $this->ftp_host";
                                return 
false;
                        }
                }
                else
                {
                        
$this->error "Invalid Connection to $this->ftp_host";
                        return 
false;
                }

                if( 
ftp_login$this->ftp_stream$this->ftp_user$this->ftp_pass ) )
                {
                        
// set passive/binary mode
                        
ftp_pasv$this->ftp_stream$this->ftp_passive_mode );

                        
$this->system_type ftp_systype$this->ftp_stream );
                        return 
true;
                }
                else
                {
                        
// connection failed
                        
$this->error "Login failed to $this->ftp_host";
                        return 
false;
                }
        }

        
/**
         * Delete file on FTP server
         *
         * @access      public
         * @param       string  $remote_file
         * @return      bool
         *
         */
        
public function delete$remote_file=null )
        {
                if( 
ftp_delete$this->ftp_stream$remote_file ) )
                {
                        return 
true;
                }
                else
                {
                        
$this->error "Unable to delete file $remote_file";
                        return 
false;
                }
        }

        
/**
         *
         * Download file from server
         *
         * @access      public
         * @param       string  $remote_file
         * @param       string  $local_file
         * @param       int     $mode
         * @return      bool
         *
         */
        
public function get$remote_file=null$local_file=null$mode=FTP_ASCII )
        {
                if( 
ftp_get$this->ftp_stream$local_file$remote_file$mode ) )
                {
                        return 
true;
                }
                
$this->error "Download failed $remote_file";
                return 
false;
        }

        
/**
         *
         * Get directory list
         *
         * @access      public
         * @param       string  $directory
         * @return      array   Array of file/dir names
         *
         */
        
public function ls$directory=null )
        {
                
$list = array();

                if( 
$list ftp_nlist$this->ftp_stream$directory ) )
                {
                        return 
$list;
                }
                
$this->error "Failed to get directory listing";
                return array();
        }

        
/**
         *
         * Create remote directory on FTP server
         *
         * @access      public
         * @param       string $directory
         * @return      bool
         *
         */
        
public function mkdir$directory=null )
        {
                if( 
ftp_mkdir$this->ftp_stream$directory ) )
                {
                        return 
true;
                }
                
$this->error "Unable to create directory $directory";
                return 
false;
        }

        
/**
         *
         * Upload file to ftp server
         *
         * @access      public
         * @param       string  $local_path
         * @param       string  $remote_file_path
         * @param       int     $mode
         * @return      bool
         *
         */
        
public function put$local_file=null$remote_file=null$mode=FTP_ASCII )
        {
                if( 
ftp_put$this->ftp_stream$remote_file$local_file$mode ) )
                {
                        return 
true;
                }
                
$this->error "Failed to upload file \"{$local_file}\"";
                return 
false;
        }

        
/**
         *
         * Get current directory
         *
         * @access      public
         * @return      string
         *
         */
        
public function pwd()
        {
                return 
ftp_pass$this->ftp_stream );
        }

        
/**
         *
         * Rename file on FTP server
         *
         * @access      public
         * @param       string  $old_name
         * @param       string  $new_name
         * @return      bool
         *
         */
        
public function rename$old_name=null$new_name=null )
        {
                if( 
ftp_rename$this->ftp_stream$old_name$new_name ) )
                {
                        return 
true;
                }
                
$this->error "Failed to rename file \"{$old_name}\"";
                return 
false;
        }

        
/**
         *
         * Remove directory on FTP server
         *
         * @access      public
         * @param       string  $directory
         * @return      bool
         *
         */
        
public function rmdir$directory=null )
        {
                if(
ftp_rmdir($this->ftp_stream$directory))
                {
                        return 
true;
                }
                
$this->error "Failed to remove directory \"{$directory}\"";
                return 
false;
        }


        
/**
         *
         * Get filesize on FTP server
         *
         * @access      public
         * @param       string $file
         * @return      bool
         *
         */
        
public function filesize$file=null )
        {
                if(
$res ftp_size($this->_stream$file))
                {
                        return 
$res;
                }
                
$this->error "Failed to get filesize \"{$file}\"";
                return 
false;
        }

// end of class

?>

Usage

<?php

$ftp_host 
'ftp.example.com';
$ftp_user 'username';
$ftp_pass 'password';

try
{
    
$ftp = new ftp$ftp_host$ftp_user$ftp_pass );
    if( 
$ftp->connect() !== false )
    {
        
$ftp->ftp_path       '/www';
        
$files $ftp->ls'/www' );
        
print_r$files );
    }
    else
    {
        echo 
$ftp->error."\n\n";
    }
}
catch( 
Exception $e )
{
    echo 
$e->getMessage();
}

?>

Other commands

Get file from directory
$ftp->get( 'remote_file', 'local_file' );
Change Directory
$ftp->cd( 'dir_name' );
Set file permissions
$ftp->chmod( 0777, 'remote_file.txt' );
Delete File
$ftp->delete( 'remote_file.txt' );
Get list of files and directories in a directory
$file_array = $ftp->ls( 'dir_name' ) );
Create a directory
$ftp->mkdir( 'new_dir' );
Get current directory
$pwd = $ftp->pwd();
Rename file
$ftp->rename( 'old_name.php', 'new_name.php' );
Remove directory
$ftp->rmdir("mydir2");
Display last FTP error
echo $ftp->error;
Set passive mode. Use before connect()
$ftp->passive = true;
Use SSL-FTP connection. Use before connect()
$ftp->ssl = true;
Set server system type (after connect() )
$system_type = $ftp->system_type;