php_ref_ftp
PHP FTP/FTPS Functions
Section titled “PHP FTP/FTPS Functions”PHP FTP Introduction
Section titled “PHP FTP Introduction”The FTP functions allow PHP scripts to act as an FTP client, enabling interaction with FTP servers. This includes opening connections, logging in, transferring files (upload/download), managing directories, and more.
These functions provide low-level access to the File Transfer Protocol (FTP). Note that not all functions may be supported by every FTP server, and behavior can sometimes vary.
Security Warning: Standard FTP transmits credentials (username/password) and data in plain text, making it insecure over public networks. Whenever possible, prioritize secure alternatives like FTPS (FTP over SSL/TLS) or SFTP (SSH File Transfer Protocol).
For simpler file read/write operations, consider using PHP’s filesystem functions with the ftp:// or ftps:// stream wrappers, which can offer a more intuitive interface (e.g., file_get_contents('ftp://user:pass@host/file.txt')). However, for more complex interactions (like listing directories, changing modes), the dedicated FTP functions are necessary.
Installation
Section titled “Installation”To use the FTP functions, the FTP extension must be enabled in your PHP installation.
- Linux Systems: Often installed via a package manager, e.g.,
sudo apt-get install php-ftp(Debian/Ubuntu) orsudo yum install php-ftp(CentOS/RHEL). You might need to restart your web server (Apache/Nginx) and PHP-FPM afterwards. - Windows Systems: The FTP extension is typically bundled and can be enabled by uncommenting
extension=ftpin yourphp.inifile and restarting the web server. - Compiling from Source: If compiling PHP manually, include the
--enable-ftpconfigure option. For FTPS support (ftp_ssl_connect()), PHP must be compiled with OpenSSL support.
You can verify if the extension is enabled by checking the output of phpinfo() or using extension_loaded('ftp').
Secure Alternatives: FTPS and SFTP
Section titled “Secure Alternatives: FTPS and SFTP”FTPS (FTP over SSL/TLS):
Section titled “FTPS (FTP over SSL/TLS):”FTPS encrypts the FTP connection using SSL/TLS. PHP’s FTP extension supports explicit FTPS using the ftp_ssl_connect() function. Ensure your PHP build includes OpenSSL support.
SFTP (SSH File Transfer Protocol):
Section titled “SFTP (SSH File Transfer Protocol):”SFTP is a different protocol that runs over SSH (Secure Shell). It’s generally considered more secure and firewall-friendly than FTPS. PHP does not have built-in SFTP support within the standard FTP extension.
To use SFTP, you typically need to install the SSH2 PECL extension. This provides functions like ssh2_connect(), ssh2_auth_password(), ssh2_sftp(), ssh2_scp_send(), and ssh2_scp_recv().
Alternatively, libraries like phpseclib provide pure-PHP implementations of SFTP and other protocols, avoiding the need for PECL extensions.
Recommendation: Use SFTP or FTPS whenever the server supports it. Avoid plain FTP unless absolutely necessary and only on trusted networks.
PHP FTP Functions Reference
Section titled “PHP FTP Functions Reference”The following functions are part of the core FTP extension:
| Function | Description |
|---|---|
| ftp_alloc() | Allocates space for a file to be uploaded. |
| ftp_cdup() | Changes to the parent directory. |
| ftp_chdir() | Changes the current directory on the server. |
| ftp_chmod() | Sets permissions on a remote file (if server allows). |
| ftp_close() | Closes the FTP connection. Alias: ftp_quit(). |
| ftp_connect() | Opens an FTP connection. (Insecure - credentials sent in plain text). |
| ftp_delete() | Deletes a file on the server. |
| ftp_exec() | Requests execution of a program on the server (often disabled). |
| ftp_fget() | Downloads a file from the server and saves to an open local file pointer. |
| ftp_fput() | Uploads data from an open local file pointer to a remote file. |
| ftp_get_option() | Retrieves runtime options for the FTP connection. |
| ftp_get() | Downloads a file from the server and saves it locally. |
| ftp_login() | Logs in to the FTP connection. (Insecure - credentials sent in plain text). |
| ftp_mdtm() | Returns the last modified time of a remote file. |
| ftp_mkdir() | Creates a directory on the server. |
| ftp_nb_continue() | Continues non-blocking file transfer. |
| ftp_nb_fget() | Downloads a file non-blockingly to an open local file pointer. |
| ftp_nb_fput() | Uploads non-blockingly from an open local file pointer. |
| ftp_nb_get() | Downloads a file non-blockingly. |
| ftp_nb_put() | Uploads a file non-blockingly. |
| ftp_nlist() | Returns a list of filenames in the specified directory. |
| ftp_pasv() | Turns passive mode on or off. (Often required for firewalls). |
| ftp_put() | Uploads a local file to the server. |
| ftp_pwd() | Returns the current directory name. |
| ftp_quit() | Alias for ftp_close(). |
| ftp_raw() | Sends an arbitrary command to the FTP server. |
| ftp_rawlist() | Returns a detailed list of files in a directory (raw server format). |
| ftp_rename() | Renames a file or directory on the server. |
| ftp_rmdir() | Removes (deletes) an empty directory on the server. |
| ftp_set_option() | Sets runtime options for the FTP connection. |
| ftp_site() | Sends a SITE command to the server. |
| ftp_size() | Returns the size of a remote file in bytes. |
| ftp_ssl_connect() | Opens a secure FTP connection over SSL/TLS (FTPS). Requires OpenSSL support in PHP. |
| ftp_systype() | Returns the system type identifier of the remote server. |
PHP Predefined FTP Constants
Section titled “PHP Predefined FTP Constants”These constants are defined by the FTP extension and primarily used for transfer modes and non-blocking operations.
| Constant | Description |
|---|---|
| FTP_ASCII | Transfer mode for text files (translates line endings). Alias: FTP_TEXT. |
| FTP_BINARY | Transfer mode for binary files (raw transfer). Alias: FTP_IMAGE. |
| FTP_TIMEOUT_SEC | Default timeout used for network operations if not set otherwise via ftp_set_option(). |
| FTP_AUTOSEEK | Used with ftp_set_option(). Enables seeking within file transfers (affects ftp_get/ftp_put with resume_pos). |
| FTP_AUTORESUME | Used with ftp_set_option(). Automatically determines resume position for GET/PUT transfers. |
| FTP_FAILED | Return status for non-blocking transfers: Transfer failed. |
| FTP_FINISHED | Return status for non-blocking transfers: Transfer finished. |
| FTP_MOREDATA | Return status for non-blocking transfers: Transfer still active. |
For detailed usage and examples, refer to the official PHP FTP documentation.