downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
view the version of this page
Last updated: Thu, 21 Aug 2003

XXXII. FTP functions


The functions in this extension implement client access to file servers speaking the File Transfer Protocol (FTP) as defined in This extension is meant for detailed access to an FTP server providing a wide range of control to the executing script. 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.


No external libraries are needed to build this extension.


In order to use FTP functions with your PHP configuration, you should add the --enable-ftp option when installing PHP 4 or --with-ftp when using PHP 3.

The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions.

Runtime Configuration

This extension has no configuration directives defined in php.ini.

Resource Types

This extension uses one resource type, which is the link identifier of the FTP connection, returned by ftp_connect().

Predefined Constants

The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.

FTP_ASCII (integer)

FTP_TEXT (integer)

FTP_BINARY (integer)

FTP_IMAGE (integer)


See ftp_set_option() for information.

The following constants were introduced in PHP 4.3.0.

FTP_AUTOSEEK (integer)

See ftp_set_option() for information.


Automatically determine resume position and start position for GET and PUT requests (only works if FTP_AUTOSEEK is enabled)

FTP_FAILED (integer)

Asynchronous transfer has failed

FTP_FINISHED (integer)

Asynchronous transfer has finished

FTP_MOREDATA (integer)

Asynchronous transfer is still active


Example 1. FTP example

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

// check connection
if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";

// close the FTP stream 

Table of Contents
ftp_cdup -- Changes to the parent directory
ftp_chdir -- Changes directories on a FTP server
ftp_chmod -- Set 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 -- Requests execution of a program on the FTP server
ftp_fget -- Downloads a file from the FTP server and saves to an open file
ftp_fput -- Uploads from an open file to the FTP server
ftp_get_option -- Retrieves various runtime behaviours of the current FTP stream
ftp_get -- Downloads a file from the FTP server
ftp_login -- Logs in to an FTP connection
ftp_mdtm -- Returns the last modified time of the given file
ftp_mkdir -- Creates a directory
ftp_nb_continue -- Continues retrieving/sending a file (non-blocking)
ftp_nb_fget -- Retrieves a file from the FTP server and writes it to an open file (non-blocking)
ftp_nb_fput -- Stores a file from an open file to the FTP server (non-blocking)
ftp_nb_get -- Retrieves a file from the FTP server and writes it to a local file (non-blocking)
ftp_nb_put -- Stores a file on the FTP server (non-blocking)
ftp_nlist -- Returns a list of files in the given directory
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 an arbitrary command to an FTP server
ftp_rawlist -- Returns a detailed list of files in the given directory
ftp_rename -- Renames a file on the FTP server
ftp_rmdir -- Removes a directory
ftp_set_option -- Set miscellaneous runtime FTP options
ftp_site -- Sends a SITE command to the server
ftp_size -- Returns the size of the given file
ftp_ssl_connect -- Opens an Secure SSL-FTP connection
ftp_systype -- Returns the system type identifier of the remote FTP server

add a note add a note User Contributed Notes
FTP functions
postmaster at alishomepage dot com
24-Oct-2003 06:06
Here's another FTP interface over PHP (also uses MySQL)

PS: this script will ALSO allow you to download its source... So it becomes interesting for YOU PROGRAMMERS as well :D
arjenjb dot wanadoo dot nl
10-Mar-2003 02:29
Check for a FTP server written in PHP.
Supports Passive and Active FTP, and all other standard FTP commands as decribed in RFC959.
php dot net at thedrewerys dot com
08-Feb-2003 07:25
To chmod, you need to use ftp_site instead of ftp_exec.

In the example above,
ftp_exec($ftp_conn, 'chmod 0666 logs/log.txt')
should be
ftp_site($ftp_conn, 'chmod 0666 logs/log.txt')
eric at evilwalrus dot com
12-Jan-2003 02:14
There is no FTP command for the CHMODing of files, so you really have to do it manually using ftp_exec(). I havn't tried this myself but I'm assuming it should work, but here is an example of what I would do.

//After we have connected to the FTP server
//and need to set the perms for a file/dir, use
//this command
if (ftp_exec($ftp_conn, 'chmod 0666 logs/log.txt'))
    echo 'Successfully set permissions for logs/log.txt to 0666';
    echo 'Failed to set permissions for logs/log.txt';

Again, I have not tested this, but I assume it would work. Hope that helps.
Marius at lowvoice dot nl
23-Oct-2002 04:51
if you need to download a  file into a  string instead of into a file try this:

$temp = tmpfile();

$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $user, $pass);

$fget_result=ftp_fget($conn_id, $temp, $ftp_file_name, FTP_ASCII);

rewind ($temp);

you could also do this with fopen() but I found that using this function will keep the connection open even after a fclose()!! So repeating this many times will cause a lot of open ftp-connections.
NOSPAMkent at ioflux dot NOSPAM dot com
20-Sep-2002 07:05
I think what some other posts were trying to say which may need clarification is that in PHP 4.2.3, ftp_connect("") was failing most of the time, except it would work like every few minutes.

The fix is that ftp_connect seems to have a bug resolving addresses. If you do:

$hostip = gethostbyname($host);
$conn_id = ftp_connect($hostip);

It seems to solve the problem.
(Other users referred to an ftpbuf() error... not sure what that is, but this should fix it.)
sven at cartell-network dot de
14-Feb-2002 03:27
connection to a ftp server across proxy

$ftp_server = "proxy"; f.e. 123.456.789.10
$ftp_user_name = "username@ftpserver"; f.e.
$ftp_user_pass = "password";

$conn_id = ftp_connect($ftp_server, 2121);
$login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
Paul Southworth
17-Mar-2000 06:27
If you are looking for a sketchy sample implementation of an FTP client written in PHP, here is a free one:

 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST