PhpFtp is a library used to perform both simple and complex FTP operations such as upload, download, delete, chmod with a very customizable set of options.
Here is the basic usage:
$options = array(
PhpFtp::OPT_HOST => 'localhost',
PhpFtp::OPT_USERNAME => 'user',
PhpFtp::OPT_PASSWORD => 'password'
);
$localDirectory = dirname(__FILE__) . '/files/';
$remoteDirectory = '/';
$ftp = new PhpFtp($options);
$ftp->connect();
$ftp->uploadDirectory($localDirectory, $remoteDirectory);
$ftp->downloadDirectory($localDirectory . 'tmp/', $remoteDirectory);
$ftp->close();
$options = array(
PhpFtp::OPT_HOST => 'localhost',
PhpFtp::OPT_PORT => 21,
PhpFtp::OPT_USERNAME => 'user',
PhpFtp::OPT_PASSWORD => 'password'
PhpFtp::OPT_USE_TMP_FILE => true,
PhpFtp::OPT_OVERWRITE => true,
PhpFtp::OPT_LOG_METHOD => 'logMessage',
PhpFtp::OPT_RESUME => true,
PhpFtp::OPT_PASV => true,
PhpFtp::OPT_MODE => FTP_BINARY,
PhpFtp::OPT_RETRY_ENABLE => true,
PhpFtp::OPT_RETRY_COUNT => 2
);
$localDirectory = dirname(__FILE__) . '/files/';
$remoteDirectory = '/';
function logMessage($message, $file, $line)
{
echo date('Ymd H:i:s') . ' - ' . $message . PHP_EOL;
}
try {
$ftp = new PhpFtp($options);
$ftp->connect();
$ftp->setTimeout(60);
$ftp->uploadFile($localDirectory . 'x/1.txt', $remoteDirectory . 'x/1.txt');
$size = $ftp->getFileSize($remoteDirectory . 'x/1.txt');
logMessage('Size: ' . $size, __FILE__, __LINE__);
$list = array(
$localDirectory . '1/',
$localDirectory . '2/1.txt'
);
$ftp->uploadList($list, $remoteDirectory);
$contextualOptions = array(
PhpFtp::OPT_OVERWRITE => false
);
$ftp->uploadDirectory($localDirectory . '3/', $remoteDirectory, $contextualOptions);
$ftp->downloadDirectory($localDirectory . '3/', $remoteDirectory);
$ftp->close();
} catch (PhpFtp_Exception $ex) {
logMessage('Error: ' . $ex->getMessage(), __FILE__, __LINE__);
}
The configuration for the PhpFtp is done using options. The options can be provided both using the constructor or using the setOptions() method:
$ftp = new PhpFtp($options);
Options can be altered during execution using the setOptions() method.
$ftp->setOptions($options);
When setting options, not all values are required, only those that need to be changed. Values are merged with the existing ones automatically.
Additionally, contextual options can be provided to upload and download methods. Contextual options apply only to the current method and they are reset to the previous values after execution (both when the execution is with success and for failure).
95Example:95
$options = array(
PhpFtp::OPT_HOST => 'localhost',
PhpFtp::OPT_USERNAME => 'user',
PhpFtp::OPT_PASSWORD => 'password'
);
// by default overwrite is disabled
$ftp = new PhpFtp($options);
// upload a directory with overwrite enabled
$contextualOptions = array(
PhpFtp::OPT_OVERWRITE => false
);
$ftp->uploadDirectory($localDirectory, $remoteDirectory, $contextualOptions);
// upload another directory with overwrite disabled
$ftp->uploadDirectory($localDirectory . '2/', $remoteDirectory);
Exceptions of type PhpFtp_Exception are thrown only when a blocking situation occurs for the script (for example when connection could not be established), for all common failure cases (for example upload failed) there is no exception thrown, the result is returned in boolean form to be handled from the outside.
125Example:125
try {
$ftp->connect();
} catch (PhpFtp_Exception $ex) {
// could not connect to FTP server
}
$try = 0;
do {
$downloadResult = $ftp->downloadDirectory($localDirectory . '3/', $remoteDirectory);
$try++;
} while (!$downloadResult && $try < 3);
Configuration is done using the options. Here is the list of available options:
PhpFtp::OPT_HOST - The FTP server address (either IP or DNS name). This parameter shouldn't have any trailing slashes and shouldn't be prefixed with ftp://. This parameter is mandatory to connect.
PhpFtp::OPT_PORT - The FTP server port. Default value is 21.
PhpFtp::OPT_USERNAME - The username to connect to the FTP server. This parameter is mandatory to connect.
PhpFtp::OPT_PASSWORD - The password to connect to the FTP server. This parameter is mandatory to connect.
PhpFtp::OPT_TIMEOUT - Timeout value in seconds. This parameter specifies the timeout for all subsequent network operations. By default it is 90 seconds.
PhpFtp::OPT_MODE - Mode used to transfer files, can be: FTP_BINARY or FTP_ASCII. Default value is FTP_BINARY.
PhpFtp::OPT_SSL - Boolean value which determines whether to user not SSL connection with the FTP server. Default value is false.
PhpFtp::OPT_PASV - Boolean value which determines whether to user not passive mode when transferring files. Passive mode is required if the FTP server is behind a firewall. By default, active mode is used.
PhpFtp::OPT_USE_TMP_FILE - Boolean value which determines whether to user not temporary files when transferring files (both upload and download). If enabled, the files are transfered to FILENAME.TMP_EXTENSION first and renamed to the final name when the transfer is finished. This is useful to know which files are still in progress (used mostly when dealing with large files). The temporary file extension is taken from PhpFtp::OPT_TMP_FILE_EXTENSION option. Default value is false.
PhpFtp::OPT_TMP_FILE_EXTENSION - Temporary file extension. See description of PhpFtp::OPT_USE_TMP_FILE for more information about temporary files behavior. Required when PhpFtp::OPT_USE_TMP_FILE is true. Default value is '.tmp'.
PhpFtp::OPT_OVERWRITE - Boolean value which determines whether to overwrite files when transferring (both upload and download). Default value is false.
PhpFtp::OPT_REMOVE_UPLOADED - Boolean value which determines whether to remove local files after successful upload. Default value is false.
PhpFtp::OPT_LOG_METHOD - The callback of a logging method. If set, this must be a valid callback: - 'loggingMethod' - function name - array($object, 'loggingMethod') - public method - array('className', 'loggingMethod') - public static method The logging method needs to accept 3 parameters: text, fileName, fileLine.
PhpFtp::OPT_RESUME - Boolean value which determines whether to resume files when transferring (both upload and download). Default value is false.
PhpFtp::OPT_RETRY_ENABLE - Boolean value which determines whether to enable or not retry mechanism. This is used only for connect and login to the FTP server. Default value is false.
PhpFtp::OPT_RETRY_COUNT - The total number of retries. This is used only when retry mechanism is enabled. Default value is 3.
PhpFtp::OPT_RETRY_SLEEP - The number of seconds to sleep between retries. This is used only when retry mechanism is enabled. Default value is 1.
To view all of the available class methods take a look at the API reference: http://bubble.ro/php/phpftp/doc/
The documentation just links you up to another source. Not very good for use offline or when you wish to work on a development localhost without connection.
Questions & Comments