Code Igniter User Guide Version 1.5.0b3


FTP Class

Code Igniter's FTP Class permits files to be uploaded via FTP to your server. It also includes a "mirroring" function that permits an local directory to be recreated remotely via FTP.

Initializing the Class

Like most other classes in Code Igniter, the FTP class is initialized in your controller using the $this->load->library function:

$this->load->library('ftp');

Once loaded, the FTP object will be available using: $this->ftp

Usage Examples

In this example a connection is opened to the FTP server, and a local file is read and uploaded in ASCII mode. The file permissions are set to 755. Note: Setting permissions requires PHP 5.

$this->load->library('ftp');

$config['hostname'] = 'ftp.your-site.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;

$this->ftp->connect($config);

$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);

$this->ftp->close();

In this example a list of files is retrieved from the server.

$this->load->library('ftp');

$config['hostname'] = 'ftp.your-site.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;

$this->ftp->connect($config);

$list = $this->ftp->list_files('/public_html/');

print_r($list);

$this->ftp->close();

In this example a local directory is mirrored on the server.

$this->load->library('ftp');

$config['hostname'] = 'ftp.your-site.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['debug'] = TRUE;

$this->ftp->connect($config);

$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');

$this->ftp->close();

Function Reference

$this->ftp->connect()

Connects and logs into to the FTP server. Connection preferences are set by passing an array to the function, or you can store them in a config file.

Here is an example showing how you set preferences manually:

$this->load->library('ftp');

$config['hostname'] = 'ftp.your-site.com';
$config['username'] = 'your-username';
$config['password'] = 'your-password';
$config['port']     = 21;
$config['passive']  = FALSE;
$config['debug']    = TRUE;

$this->ftp->connect($config);

Setting FTP Preferences in a Config File

If you prefer you can store your FTP preferences in a config file. Simply create a new file called the ftp.php, add the $config array in that file. Then save the file at config/ftp.php and it will be used automatically.

Available connection options:

$this->ftp->sconnect()

Secure FTP connect. This function is identical to the function above, except that it initiates a secure connection.

$this->ftp->upload()

Uploads a file to your server. You must supply the local path and the remote path, and you can optionally set the mode and permissions. Example:

$this->ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775);

Mode options are:  ascii, binary, and auto (the default). If auto is used it will base the mode on the file extension of the source file.

Permissions are available if you are running PHP 5 and can be passed as an octal value in the fourth parameter.

$this->ftp->mkdir()

Lets you create a directory on your server. Supply the path ending in the folder name you wish to create, with a trailing slash:

// Creates a folder named "bar"
$this->ftp->mkdir('/public_html/foo/bar/');

$this->ftp->chmod()

Permits you to set file permissions. Supply the path to the file or folder you wish to alter permissions on:

// Chmod "bar" to 777
$this->ftp->chmod('/public_html/foo/bar/', 0777);

$this->ftp->list_files()

Permits you to retrieve a list of files on your server returned as an array. You must supply the path to the desired directory.

$list = $this->ftp->list_files('/public_html/');

print_r($list);

$this->ftp->mirror()

Recursively reads a folder and everything it contains (including sub-folders) and creates a mirror via FTP based on it. Whatever the directory structure of the original file path will be recreated on the server. You must supply a source path and a destination path:

$this->ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');

$this->ftp->close();

Closes the connection to your server. It's recommended that you use this when you are finished uploading.