Ftp function in php

PHP FTP Functions

The FTP functions give client access to file servers through the File Transfer Protocol (FTP).

The FTP functions are used to open, login and close connections, as well as upload, download, rename, delete, and get information on files from file servers. Not all of the FTP functions will work with every server or return the same results. The FTP functions became available with PHP 3.

If you only wish to read from or write to a file on an FTP server, consider using the ftp:// wrapper with the Filesystem functions which provide a simpler and more intuitive interface.

Installation

For these functions to work, you have to compile PHP with —enable-ftp.

The Windows version of PHP has built-in support for this extension.

PHP FTP Functions

Function Description
ftp_alloc() Allocates space for a file to be uploaded to the FTP server
ftp_cdup() Changes to the parent directory on the FTP server
ftp_chdir() Changes the current directory on the FTP server
ftp_chmod() Sets permissions on a file via FTP
ftp_close() Closes an FTP connection
ftp_connect() Opens an FTP connection
ftp_delete() Deletes a file on the FTP server
ftp_exec() Executes a command on the FTP server
ftp_fget() Downloads a file from the FTP server and saves it into an open local file
ftp_fput() Uploads from an open file and saves it to a file on the FTP server
ftp_get() Downloads a file from the FTP server
ftp_get_option() Returns runtime options of the FTP connection
ftp_login() Logs in to the FTP connection
ftp_mdtm() Returns the last modified time of a specified file
ftp_mkdir() Creates a new directory on the FTP server
ftp_mlsd() Returns the list of files in the specified directory
ftp_nb_continue() Continues retrieving/sending a file (non-blocking)
ftp_nb_fget() Downloads a file from the FTP server and saves it into an open file (non-blocking)
ftp_nb_fput() Uploads from an open file and saves it to a file on the FTP server (non-blocking)
ftp_nb_get() Downloads a file from the FTP server (non-blocking)
ftp_nb_put() Uploads a file to the FTP server (non-blocking)
ftp_nlist() Returns a list of files in the specified directory on the FTP server
ftp_pasv() Turns passive mode on or off
ftp_put() Uploads a file to the FTP server
ftp_pwd() Returns the current directory name
ftp_quit() Alias of ftp_close()
ftp_raw() Sends a raw command to the FTP server
ftp_rawlist() Returns a list of files with file information from a specified directory
ftp_rename() Renames a file or directory on the FTP server
ftp_rmdir() Deletes an empty directory on the FTP server
ftp_set_option() Sets runtime options for the FTP connection
ftp_site() Sends an FTP SITE command to the FTP server
ftp_size() Returns the size of the specified file
ftp_ssl_connect() Opens a secure SSL-FTP connection
ftp_systype() Returns the system type identifier of the FTP server
Читайте также:  Javascript strict type checking

PHP Predefined FTP Constants

Constant Type Description
FTP_ASCII Integer
FTP_AUTOSEEK Integer
FTP_AUTORESUME Integer
FTP_BINARY Integer
FTP_FAILED Integer Asynchronous transfer has failed
FTP_FINISHED Integer Asynchronous transfer is completed
FTP_IMAGE Integer Alias of FTP_BINARY
FTP_MOREDATA Integer Asynchronous transfer is in progress
FTP_TEXT Integer Alias of FTP_ASCII
FTP_TIMEOUT_SEC Integer The timeout used for network operations
FTP_USEPASVADDRESS Boolean

Источник

Ftp function in php

For those who dont want to deal with handling the connection once created, here is a simple class that allows you to call any ftp function as if it were an extended method. It automatically puts the ftp connection into the first argument slot (as all ftp functions require).

public function __construct ( $url ) <
$this -> conn = ftp_connect ( $url );
>

public function __call ( $func , $a ) <
if( strstr ( $func , ‘ftp_’ ) !== false && function_exists ( $func )) <
array_unshift ( $a , $this -> conn );
return call_user_func_array ( $func , $a );
>else <
// replace with your own error handler.
die( » $func is not a valid FTP function» );
>
>
>

// Example
$ftp = new ftp ( ‘ftp.example.com’ );
$ftp -> ftp_login ( ‘username’ , ‘password’ );
var_dump ( $ftp -> ftp_nlist ());
?>

Upload file to server via ftp.

$ftp_server = «» ;
$ftp_user_name = «» ;
$ftp_user_pass = «» ;
$file = «» ; //tobe uploaded
$remote_file = «» ;

// set up basic connection
$conn_id = ftp_connect ( $ftp_server );

// login with username and password
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );

// upload a file
if ( ftp_put ( $conn_id , $remote_file , $file , FTP_ASCII )) <
echo «successfully uploaded $file \n» ;
exit;
> else <
echo «There was a problem while uploading $file \n» ;
exit;
>
// close the connection
ftp_close ( $conn_id );
?>

In example 2 above you may need to set the system to to use pasv to get a result ie:

$ftp = new ftp(‘ftp.example.com’);
$ftp->ftp_login(‘username’,’password’);
$ftp->ftp_pasv(TRUE);
var_dump($ftp->ftp_nlist());

syntax error in the above example, ftp_nlist requires a directory parameter:

$ftp->ftp_nlist(‘.’); // retrieve contents of current directory

Источник

ftp_connect

ftp_connect() opens an FTP connection to the specified hostname .

Parameters

The FTP server address. This parameter shouldn’t have any trailing slashes and shouldn’t be prefixed with ftp:// .

This parameter specifies an alternate port to connect to. If it is omitted or set to zero, then the default FTP port, 21, will be used.

This parameter specifies the timeout in seconds for all subsequent network operations. If omitted, the default value is 90 seconds. The timeout can be changed and queried at any time with ftp_set_option() and ftp_get_option() .

Return Values

Returns an FTP\Connection instance on success, or false on failure.

Changelog

Examples

Example #1 ftp_connect() example

// set up a connection or die
$ftp = ftp_connect ( $ftp_server ) or die( «Couldn’t connect to $ftp_server » );

See Also

User Contributed Notes 3 notes

Ever needed to create an FTP connection resource defaulted to a particular dir from a URI? Here’s a simple function that will take a URI like ftp://username:password@subdomain.example.com/path1/path2/, and return an FTP connection resource.

function getFtpConnection ( $uri )
<
// Split FTP URI into:
// $match[0] = ftp://username:password@sld.domain.tld/path1/path2/
// $match[1] = ftp://
// $match[2] = username
// $match[3] = password
// $match[4] = sld.domain.tld
// $match[5] = /path1/path2/
preg_match ( «/ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)/i» , $uri , $match );

// Set up a connection
$conn = ftp_connect ( $match [ 1 ] . $match [ 4 ] . $match [ 5 ]);

// Login
if ( ftp_login ( $conn , $match [ 2 ], $match [ 3 ]))
<
// Change the dir
ftp_chdir ( $conn , $match [ 5 ]);

// Return the resource
return $conn ;
>

// Or retun null
return null ;
>
?>

Sean’s example is wrong, because it includes the protocol match, so result would be php_network_getaddresses: getaddrinfo failed: Name or service not known

Источник

Оцените статью