FTP Class
The FTP class allows you to upload, download, move and mirror files with remote servers over the FTP protocol.
Configuration
The FTP class is configured through the fuel/core/config/ftp.php configuration file. It is already populated with a default configuration group. You can override this configuration and add new groups by copying this config file to your application config directory, and modify that file as needed.
The following configuration settings can be defined:
hostname |
string |
'localhost'
|
IP or domain name of the FTP server to connect with.
|
username |
string |
''
|
Optional: Name of the user to connect with, if login is required.
|
password |
string |
''
|
Optional: Name of the password to connect with, if login is required.
|
port |
integer |
21
|
The port number your FTP server responds to. Most servers use 21.
|
passive |
boolean |
true
|
Whether to use passive mode. Passive is set automatically by default.
|
ssl_mode |
boolean |
false
|
Use FTPS mode which is slightly more secure than usual FTP. (Note: this is not SFTP).
|
debug |
boolean |
false
|
Whether to enable debugging to display error messages.
|
factory()
The factory method is used to create a new instance of the FTP class and can either reference a different config group
or be passed an array of configuration options.
Static |
Yes |
Parameters |
Param |
Default |
Description |
$config |
'default'
|
The name of the config group to use, or a configuration array. |
$connect |
true
|
Automatically connect to this server. |
|
Returns |
Ftp object |
Example |
// Connect to the default server
$ftp = Ftp::factory();
// Connect to a predefined server defined in config/ftp.php
$ftp2 = Ftp::factory('image-storage');
// Connect to a server on-the-fly
$ftp3 = Ftp::factory(array(
'hostname' => 'myspace.com',
'username' => '',
'password' => '',
'port' => 21,
'passive' => true,
'ssl_mode' => false,
'debug' => false
));
if ($ftp3->delete_dir('/') === true)
{
exit('The world owes you a debt of gratitude');
}
else
{
exit('You tried and that is the main thing.);
}
|
change_dir($path = '')
The change_dir changes the "current directory".
Static |
No |
Parameters |
Param |
Default |
Description |
$path |
''
|
Remote path to move to. |
|
Returns |
boolean |
Example |
// Move to /user/phil/public_html/some/path/
$ftp->change_dir('/public_html/some/path/');
|
mkdir($path, $permissions = null)
The mkdir method is used to create a new directory on the remote server.
Static |
No |
Parameters |
Param |
Default |
Description |
$path |
Required |
Remote directory to create. |
$permissions |
null
|
If set the permissions will be applied to the directory. |
|
Returns |
boolean |
Example |
// Make a write-able uploads folder
$ftp->mkdir('/public_html/uploads/', 0777);
|
upload($local_path, $remote_path, $mode = 'auto', $permissions = null)
Upload a file or directory from the local server to the remote server.
Static |
No |
Parameters |
Param |
Default |
Description |
$local_path |
'default'
|
Local server path. |
$remote_path |
true
|
Remote server path. |
$mode |
'auto'
|
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 |
null
|
If set the permissions will be applied to the directory. |
|
Returns |
boolean |
Example |
// Upload myfile.html
$ftp->upload('/local/path/to/myfile.html', '/public_html/myfile.html', 'ascii', 0775)
|
download($remote_path, $local_path, $mode = 'auto')
Download a file or directory from the remote server to the local server.
Static |
No |
Parameters |
Param |
Default |
Description |
$remote_path |
true
|
Remote server path. |
$local_path |
'default'
|
Local server path. |
$mode |
'auto'
|
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.
|
|
Returns |
boolean |
Example |
// Download myotherfile.html
$ftp->download('/public_html/myfile.html', '/local/path/to/myfile.html', 'ascii')
|
mirror($local_path, $remote_path)
Recursively reads a local 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.
Static |
No |
Parameters |
Param |
Default |
Description |
$local_path |
'default'
|
Local server path. |
$remote_path |
true
|
Remote server path. |
|
Returns |
boolean |
Example |
$ftp = Ftp::factory();
$ftp->connect($config);
$ftp->mirror('/path/to/myfolder/', '/public_html/myfolder/');
$ftp->close();
|