PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Networking » Net_FTP » Manual

Net_FTP provides comfortable communication with FTP-Servers. It mainly provides an OO wrapper to PHP's integrated FTP functions, adding some missing features like recursive up- and downloading of complete folders, as well as deleting. The options for generating directory listings were also extended to feature well structured listing of directories and/or files.

Constants

Constants – predefined constants

NET_FTP_FILES_ONLY

Makes Net_FTP::ls() return a structures array of files (no directories) in a directory.

NET_FTP_DIRS_ONLY

Makes Net_FTP::ls() return a structures array of directories (no files) in a directory.

NET_FTP_DIRS_FILES

Makes Net_FTP::ls() return a structures array of directories and files in a directory.

NET_FTP_RAWLIST

Makes Net_FTP::ls() return an unstructred array as returned by the PHP function ftp_raw_list().

Net_FTP::Net_FTP()

Net_FTP::Net_FTP() – constructor

Synopsis

require_once 'Net/FTP.php';

object Net_FTP::Net_FTP ( string $host = null , int $port = null )

Description

Create a new object for communication with FTP servers.

Parameter

Return value

object - the new Net_FTP object.

Note

This function can not be called statically.

Example

Using Net_FTP()

<?php

  require_once 'Net/FTP.php';

  $test = new Net_FTP('ftp.mydomain.com', 21);

?>

Net_FTP::connect()

Net_FTP::connect() – connects to a given FTP server

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::connect ( string $host = null , int $port = null )

Description

Create a new object for communication with FTP servers.

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

The returned PEAR_Error object in case of an error is unspecific. You can ignore the errornumber and errormessage, because only "Connection failed" will be returned if the connection fails.

Note

This function can not be called statically.

Example

Using connect()

<?php
     
    $test->connect('192.168.0.1', 21);

?>

Net_FTP::disconnect()

Net_FTP::disconnect() – disconnects from the FTP server

Synopsis

require_once 'Net/FTP.php';

void Net_FTP::disconnect ( )

Description

Disconnect from the FTP server you're connected to.

Return value

void

Note

This function can not be called statically.

Example

Using disconnect()

<?php
     
    $test->disconnect();

?>

Net_FTP::login()

Net_FTP::login() – logs into the FTP server you are connected to

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::login ( string $username = null , int $password = null )

Description

Does the login on the FTP server you are connected to. for that you first have to connect to a FTP server before logging in. Username and Password can either be set by the parameters or manually before (using the set-methods).

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

The returned PEAR_Error object in case of an error is unspecific. You can ignore the errornumber and errormessage, because only "Login failed" will be returned if the login fails.

Note

This function can not be called statically.

Example

Using login()

<?php
     
    $test->login('myuser', 'mypass');

?>

Net_FTP::cd()

Net_FTP::cd() – changes the directory on the server.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::cd ( string $directory )

Description

Changes the directory on the FTP server you are logged in. The parameter can either be an absolute path (e.g. '/home/mydir') or a relative one (e.g. 'mydir').

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

The returned PEAR_Error object in case of an error is unspecific. You can ignore the errornumber and errormessage, because only "Directory change failed" will be returned if the operation fails.

Note

This function can not be called statically.

Example

Using cd()

<?php
     
    $test->cd('../mydir');

?>

Net_FTP::pwd()

Net_FTP::pwd() – returns the directory on the server you are currently in.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::pwd ( )

Description

Returns the directory currently selected on the server.

Return value

mixed - path (absolute) as string on success, otherwise PEAR::Error.

Throws

The returned PEAR_Error object in case of an error is unspecific. You can ignore the errornumber and errormessage, because only "Could not determine the actual path" will be returned if the operation fails.

Note

This function can not be called statically.

Example

Using pwd()

<?php

  echo $test->pwd();

?>

Net_FTP::mkdir()

Net_FTP::mkdir() – creates a new directory.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::mkdir ( string $directory , bool $recursive = false )

Description

Creates a new directory on the FTP server. You can give this function either a relative or an absolute path as the first parameter. The second (optional) parameter lets you create directories recursively (meaning you can create './my/new/dir' even if '/my' doesn't exist. All 3 directories would be created ).

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

The returned PEAR_Error object in case of an error is specific. There is only one errornumber which may occur. The message given by this error will contain the directory where the error occurred in "Creation of '$dir' failed". operation fails.

Note

This function can not be called statically.

Example

Using mkdir()

<?php

  $test->mkdir('../mydir');

?>

Net_FTP::execute()

Net_FTP::execute() – executes a command on the server.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::execute ( string $command )

Description

Executes a given command on the server (the SITE EXEC command is added by the method).

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

The returned PEAR_Error object in case of an error is specific. There is only one errornumber which may occur. The message given by this error will contain the directory where the error occurred in "Execution of command '$command' failed". operation fails.

Note

This function can not be called statically.

Example

Using execute()

<?php
     
    $test->execute($myCommand);

?>

Net_FTP::mdtm()

Net_FTP::mdtm() – returns the last modification date of a file.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::mdtm ( string $file , string $format = null )

Description

gives you the last modification-date of a file either as a unix timestamp or in a formated date.

Parameter

Return value

mixed - the last modification date on success, otherwise PEAR::Error.

Throws

Several errors may be returned by mdtm. The errornumber is unspecific (until now) and will not tell you anything about the errormessage. Possible errors are:

Possible PEAR_Error values
Error message Description Solution
Filename '$file' seems to be a directory. The filename you gave the method does not reference a regular-file but a directory. Specify a correct filenamepath (eg. /my/file/path/foo.html, ../foo.html).
Could not get last-modification-date of '$file'. The last-modification date could not be determined by PHP. Reasons for this may be that your FTP-server does not support the used command or that you gave the function a non existent file as reference.
  • check the given file for existence
  • contact the FTP server's admin
Date-format failed on timestamp '$res'. The given format-string was not well formated. Check the documentation of the PHP function date().

Note

This function can not be called statically.

Example

Using mdtm()

<?php

  var_dump($test->mdtm('/foo/bar'));

  // returns the last modification time in german timeformat
  var_dump($test->mdtm('/foo/bar', 'd.m.Y, H:i'));

?>

Net_FTP::size()

Net_FTP::size() – returns the size in bytes of a file.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::size ( string $file )

Description

gives you the size of a file bytes.

Parameter

Return value

mixed - the size of the given file on success, otherwise PEAR::Error.

Throws

The returned PEAR_Error object in case of an error is unspecific. You can ignore the errornumber and errormessage, because only "Connection failed" will be returned if the connection fails.

Note

This function can not be called statically.

Example

Using size()

<?php
     
    var_dump($test->size('/foo/bar'));
    
?>

Net_FTP::ls()

Net_FTP::ls() – returns the listing of a directory in a specified way.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::ls ( string $dir = null , string $mode = NET_FTP_DIRS_FILES )

Description

this function gives you a listing of either the files / directories / both or an unformated array (like the PHP function ftp_rawlist()).

Parameter

Return value

mixed - a directory listing in the form you determine on success, otherwise PEAR::Error.

Throws

Several errors may be returned by ls. The errornumber is unspecific (until now) and will not tell you anything about the errormessage. Possible errors are:

Possible PEAR_Error values
Error message Description Solution
Raw directory-list in wrong format. The format given by the server on PHP function ftp_rawlist()was wrong. Check if the directory you wanted to be listed is correct and you have access to listing it. Specify a correct directory path (eg. /my/file/path/, ../) and check (maybe change) the rights on it.
Could not get last-modification-date of '$file'. The last-modification date could not be determined by PHP. Reasons for this might be that your FTP-server does not support the used command or that you gave the function a non existent file as reference.
  • check the given file for existence
  • contact the FTP server's admin
Date-format failed on timestamp '$res'. The given format-string was not well formated. Check the documentation of the PHP function date().

Note

This function can not be called statically.

Example

Using ls()

<?php

  var_dump($test->ls('/foo/bar'));

?>

Net_FTP::rm()

Net_FTP::rm() – deletes a file or directory.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::rm ( string $path = null , string $recursive = false )

Description

This method deletes a file or a directory.

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

Several errors may be returned by rm. The errornumber is unspecific (until now) and will not tell you anything about the errormessage. Possible errors are:

Possible PEAR_Error values
Error message Description Solution
Could not delete file '$file'. The file named $file (complete path) could not be deleted. Either the file does not exist or you do not have the permission to delete it. So check if the file exists and you have permissions on it.
Directory name '$dir' is invalid, has to end with '/' You entered a directory for deletion ($recursive = true) without ending '/'. Correct your parameter.
Could not delete directory '$dir'. The directory $dir could not be deleted. Maybe the directory is not empty ($recursive = false) or you do not have the permission to delete the directory.

Note

This function can not be called statically.

Example

Using rm()

<?php
     
    var_dump($test->rm('/foo/bar/', true));
    
?>

Net_FTP::get()

Net_FTP::get() – download a file to the computer your script runs on.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::get ( string $remote_file , string $local_file , bool $overwrite = false , int $mode = null )

Description

This downloads a file from the FTP server to the computer your script runs on.

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

Several errors may be returned by get. The errornumber is unspecific (until now) and will not tell you anything about the errormessage. Possible errors are:

Possible PEAR_Error values
Error message Description Solution
Local file '$local_file' exists and may not be overwriten. The local file you specified exists and you did not specify to overwrite it. Set parameter $overwrite = true.
Local file '$file' is not writeable. Can not overwrite. You specified to overwrite the localfile. This did not work. Maybe you don't have the permission to overwrite the file. Check the filepermissions.
File '$remote_file' could not be downloaded to '$local_file'. The download of the remote file failed. This may have several reasons: Maybe the remote file does not exist or the local directory you wanted to download to does not exist or is not writeable.

Note

This function can not be called statically.

Example

Using get()

<?php
     
    var_dump($test->get('foo/bar.zip', '/tmp/downloaded.zip', true, FTP_BINARY));
    
?>

Net_FTP::put()

Net_FTP::put() – upload a file to the FTP server.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::put ( string $local_file , string $remote_file , bool $overwrite = false , int $mode = null )

Description

This uploads a file to the FTP server from the computer your script runs on.

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

Several errors may be returned by put. The errornumber is unspecific (until now) and will not tell you anything about the errormessage. Possible errors are:

Possible PEAR_Error values
Error message Description Solution
Local file '$local_file' does not exist. The local file you specified does not exist. Correct the local file path.
Remote file '$remote_file' exists and may not be overwriten. The specified remote file exists but may not be overwritten. Maybe you don't have the permission to overwrite the file. Check the filepermissions.
File '$local_file' could not be uploaded to '$remote_file'. The upload of the local file failed. This may have several reasons: Maybe the local file does not exist or the remote directory you wanted to upload to does not exist or is not writeable.

Note

This function can not be called statically.

Example

Using put()

<?php

  var_dump($test->put('/tmp/downloaded.zip', 'foo/bar.zip', true, FTP_BINARY));

?>

Net_FTP::getRecursive()

Net_FTP::getRecursive() – download a whole directory to the computer your script runs on.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::getRecursive ( string $remote_path , string $local_path , bool $overwrite = false , int $mode = null )

Description

This downloads a whole directory from the FTP server to the computer your script runs on.

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

Several errors may be returned by getRecursive. The errornumber is unspecific (until now) and will not tell you anything about the errormessage. Possible errors are:

Possible PEAR_Error values
Error message Description Solution
Given remote-path '$remote_path' seems not to be a directory. The path you specified on the FTP sever seems not to be a valid directory node. Maybe your path does not end with '/' or the directory does not exist.
Given local-path '$local_path' seems not to be a directory. The path you specified on the local host seems not to be a valid directory node. Maybe your path does not end with '/' or the directory does not exist.
Could not create dir '$local_path'. The given directory could not be created. Check your permissions on the source-directory.
Could not create dir '$local_path'. The given directory could not be created. Check your permissions on the source-directory.

Note

This function can not be called statically.

Example

Using getRecursive()

<?php
     
    var_dump($test->getRecursive('foo/', '/tmp/foo/', true));
    
?>

Net_FTP::putRecursive()

Net_FTP::putRecursive() – upload a whole directory to the FTP server.

Synopsis

require_once 'Net/FTP.php';

mixed Net_FTP::putrecursive ( string $local_path , string $remote_path , bool $overwrite = false , int $mode = null )

Description

This uploads a whole directory to the FTP server from the computer your script runs on.

Parameter

Return value

mixed - true on success, otherwise PEAR::Error.

Throws

Several errors may be returned by putRecursive. The errornumber is unspecific (until now) and will not tell you anything about the errormessage. Possible errors are:

Possible PEAR_Error values
Error message Description Solution
Given local-path '$local_path' seems not to be a directory. The local path you have specified does not seem to be a directory. Correct the local directory path. (Does it end with '/'?)
Given remote-path '$remote_path' seems not to be a directory. The remote path you have specified does not seem to be a directory. Correct the local directory path. (Does it end with '/'?)

Note

This function can not be called statically.

Example

Using putRecursive()

<?php

  var_dump($test->putRecursive('/tmp/foo/', 'foo/', true));

?>

Net_FTP::checkFileExtension()

Net_FTP::checkFileExtension() – check extensions.ini for the transfermode of a specific file.

Synopsis

require_once 'Net/FTP.php';

integer Net_FTP::checkfileextension ( string $filename )

Description

This method checks a given filename for its proper transfermode (using extensions.ini). If the file extension can not be found, the class falls back to the standard transfer mode (attribute).

Parameter

Return value

int - either FTP_ASCII or FTP_BINARY.

Throws

No errors. Always a filetransfermode should be returned.

Note

This function can not be called statically.

Example

Using checkFileExtension()

<?php
     
    var_dump($test->checkfileextension('foo/bar.zip'));
    
?>