PHP FTP Functions

PHP FTP Introduction

FTP functions provide client access to file servers via the File Transfer Protocol (FTP).

FTP functions are used to open, log in, and close connections, as well as for uploading, downloading, renaming, deleting, and obtaining file information on the file server. Not all FTP functions work on every server or return the same results. FTP functions have been available since PHP 3.

These functions are used for detailed access to FTP servers. If you only need to perform read and write operations on FTP servers, it is recommended to use the ftp:// wrapper in the Filesystem functions.

Install

The Windows version of PHP has built-in support for the FTP extension module. No additional extension libraries need to be loaded to use these functions.

However, if you are running a PHP Linux version, please add the --enable-ftp option (PHP 4 or above version) or --with-ftp (PHP 3 version) when compiling.

PHP FTP Functions

PHP:Indicates the earliest PHP version that supports this function.

Function Description PHP
ftp_alloc() Allocates space for the file to be uploaded to the FTP server. 5
ftp_cdup() Changes the current directory to the parent directory on the FTP server. 3
ftp_chdir() Changes the current directory on the FTP server. 3
ftp_chmod() Sets file permissions on the FTP via FTP. 5
ftp_close() Closes the FTP connection. 4
ftp_connect() Opens an FTP connection. 3
ftp_delete() Deletes a file on the FTP server. 3
ftp_exec() Executes a program/command on FTP. 4
ftp_fget() Downloads a file from the FTP server and saves it into a locally opened file. 3
ftp_fput() Uploads an open file and saves it as a file on the FTP server. 3
ftp_get_option() Returns various different option settings of the current FTP connection. 4
ftp_get() Downloads a file from the FTP server. 3
ftp_login() Logs in to the FTP server. 3
ftp_mdtm() Returns the last modified time of the specified file. 3
ftp_mkdir() Creates a new directory on the FTP server. 3
ftp_nb_continue() Continuous file retrieval/send (non-blocking). 4
ftp_nb_fget() Downloads a file from the FTP server and saves it into a locally opened file (non-blocking) 4
ftp_nb_fput() Uploads an open file and saves it as a file on the FTP server (non-blocking). 4
ftp_nb_get() Downloads a file from the FTP server (non-blocking). 4
ftp_nb_put() Uploads a file to the server (non-blocking). 4
ftp_nlist() Returns the file list of the specified directory. 3
ftp_pasv() Returns whether the current FTP passive mode is enabled. 3
ftp_put() Upload a file to the server. 3
ftp_pwd() Returns the current directory name. 3
ftp_quit() Alias of ftp_close() 3
ftp_raw() Send a raw command to the FTP server. 5
ftp_rawlist() Returns a detailed list of files in the specified directory. 3
ftp_rename() Rename a file or directory on the FTP server. 3
ftp_rmdir() Delete a directory on the FTP server. 3
ftp_set_option() Set various FTP runtime options. 4
ftp_site() Send a SITE command to the server. 3
ftp_size() Returns the size of the specified file. 3
ftp_ssl_connect() Open a secure SSL-FTP connection. 4
ftp_systype() Returns the system type identifier of the remote FTP server. 3

PHP FTP Constants

PHP:Indicates the earliest PHP version that supports this constant.

Constants Description PHP
FTP_ASCII   3
FTP_TEXT   3
FTP_BINARY   3
FTP_IMAGE   3
FTP_TIMEOUT_SEC   3
FTP_AUTOSEEK   4
FTP_AUTORESUME

Automatically decide the recovery and start position for GET and PUT requests

Can only work when FTP_AUTOSEEK is opened

4
FTP_FAILED Asynchronous transfer failed 4
FTP_FINISHED Asynchronous transfer succeeded 4
FTP_MOREDATA Asynchronous transfer is in active state 4