PhpFtp

Released 6 years ago , Last update 6 years ago

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.

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.

3.0
  • 5 0
  • 4 0
  • 3 1
  • 2 0
  • 1 0
1 Reviews Read Reviews

Pricing

14 day 14-day money-back guarantee

FREE

Basic License

  • Perpetual license

  • 1 site, unlimited servers

  • No distribution (hosted use only)

  • Commercial use

Requirements

  • PHP 5
  • FTP support

Features

  • timeout customization
  • SSL support
  • passive mode support
  • automatic retries
  • overwrite for both upload and download
  • resume support for both upload and download
  • temporary file name on upload and download
  • logging support
  • contextual options

Basic Usage

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();

Complex Usage

$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__);
}

Options

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.

Contextual options

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

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

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::OPTMODE - Mode used to transfer files, can be: FTPBINARY or FTPASCII. Default value is FTPBINARY.

  • 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::OPTUSETMPFILE - 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.TMPEXTENSION 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::OPTTMPFILE_EXTENSION option. Default value is false.

  • PhpFtp::OPTTMPFILEEXTENSION - Temporary file extension. See description of PhpFtp::OPTUSETMPFILE for more information about temporary files behavior. Required when PhpFtp::OPTUSETMP_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::OPTREMOVEUPLOADED - Boolean value which determines whether to remove local files after successful upload. Default value is false.

  • PhpFtp::OPTLOGMETHOD - 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::OPTRETRYENABLE - 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::OPTRETRYCOUNT - The total number of retries. This is used only when retry mechanism is enabled. Default value is 3.

  • PhpFtp::OPTRETRYSLEEP - The number of seconds to sleep between retries. This is used only when retry mechanism is enabled. Default value is 1.

Documentation

To view all of the available class methods take a look at the API reference: http://bubble.ro/php/phpftp/doc/

2 licenses, starting from From » FREE View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.


Or enter your name and Email
No comments have been posted yet.