Important facts about commercial licenses

  • Licenses are perpetual. They do not expire and do not need to be renewed.
  • Licenses can be upgraded. You can upgrade to a more expensive license later paying only the difference in cost.
  • Pay attention to the distribution type - Hosted (sites / servers), binary (applications) or source (includes all the others). Choose according to your needs (more below).
  • All licenses allow commercial use unless otherwise indicated.
  • Read the full license by clicking on the icon.
  • Read more about licenses in our handy license guide.
Free

Basic License

1 site, unlimited servers No source distribution
$19

Support Provided

1 site, unlimited servers No source distribution 1 year support
You need to log-in or create an account
  • Create an account
  • Log-in
  • Please use your real name.
  • Account activation link will be sent to this address.
  • Minimum 8 characters

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

  • Released: May 8, 2012
    Last Update: May 8, 2012
  • Language: PHP
  • Time / costs savings: 16h / $960 *
(1 ratings)

PhpFtp

PhpFtp
Developed by Cristian Radu, Released May 8, 2012

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.

PHP

Tags: connector , ftp , php ftp

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.

Back to top

Requirements

  • PHP 5
  • FTP support
Back to top

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
Back to top

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();
Back to top

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__);
}
Back to top

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.

Back to top

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);
Back to top

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);
Back to top

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::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.

Back to top

Documentation

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

View all 1 reviews »

User Reviews

  • James Lowthorpe 10 months ago
    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.
    Flag
    Was this helpful? Yes No
Read all 2 comments »

Questions & Comments


Or enter your name and Email
  • James Lowthorpe 10 months ago
    This is pretty neat. Would you recommend that this be used from a secure log in section or what it be a huge security risk to have this open?
    • Cristian Radu Developer 10 months ago
      The class itself has been thoroughly tested and shouldn't have any problem to be used in a large project, however keep in mind that FTP protocol itself used without authentication can lead to many problems on a server:

      * disk storage - storage is not free after all
      * content issues - imagine if someone starts using your service to host warez or porn

      The FTP protocol is best used for secure transfer, for example server synchronization on a secure channel.
You must be logged-in to vote. Log-in to your account or register now.