The PHP FTP extension lets a script act as an FTP client. It can open a connection to an FTP server, sign in, move files in both directions, and manage folders and files on the remote side.
This page explains the ideas. The FTP Functions index lists all 36 function guides.
Passive mode first.
If a connection logs in fine but uploads or directory listings hang, call ftp_pasv($ftp, true) right after ftp_login(). It fixes most firewall and NAT problems.
Plain FTP is not encrypted.
With ftp_connect() your password and files cross the network as readable text. Use ftp_ssl_connect() (FTP over TLS) when the server supports it.
Syntax
PHP Syntax
The usual life cycle of an FTP session:
PHP
// 1. Open the connection (plain or SSL)
$ftp = ftp_connect($host, 21, 30); // or ftp_ssl_connect(...)
// 2. Sign in and use passive mode
ftp_login($ftp, $user, $pass);
ftp_pasv($ftp, true);
// 3. Transfer files
ftp_put($ftp, $remoteFile, $localFile, FTP_BINARY); // upload
ftp_get($ftp, $localFile, $remoteFile, FTP_BINARY); // download
// 4. Close the connection
ftp_close($ftp);
// ftp_connect() / ftp_ssl_connect():
// an FTP connection object (resource before PHP 8.1) on success
// false when the connection cannot be opened
// ftp_login(), ftp_put(), ftp_get(): true on success, false on failure
Reference
Core Ideas
What to remember when you use FTP from PHP:
Idea
Example
Remember
Connect
ftp_connect($host, 21, 30)
Returns false on failure; the third argument is the timeout in seconds
Log in
ftp_login($ftp, $user, $pass)
Required before any other command; returns true or false
Passive mode
ftp_pasv($ftp, true)
Call it after login; avoids firewall and NAT trouble
Upload
ftp_put(), ftp_fput(), ftp_append()
Arguments are remote name first, then local file
Download
ftp_get(), ftp_fget()
Arguments are local name first, then remote file
Transfer mode
FTP_BINARY, FTP_ASCII
Use binary for everything except plain text
Directories
ftp_pwd(), ftp_chdir(), ftp_mkdir(), ftp_nlist()
Work on the remote server, not on your own disk
SSL vs plain
ftp_ssl_connect() vs ftp_connect()
SSL encrypts login and data; plain does not
Code
PHP Examples
Three short scripts that show how the FTP extension is used. The host, user, and password are placeholders; replace them with your own server details.
Example 1 — Connect, log in, and enable passive mode
Open a connection, sign in, switch to passive mode, and print the remote working directory.
The FTP extension is a set of ftp_* functions that let a PHP script talk to a File Transfer Protocol server. You can connect, log in, upload and download files, list and change remote directories, and run FTP commands without any outside library.
Open the connection with ftp_connect() or ftp_ssl_connect(), authenticate with ftp_login(), usually call ftp_pasv() to turn on passive mode, do your work (ftp_put(), ftp_get(), ftp_nlist(), and so on), and finally close the connection with ftp_close().
In active mode the server opens the data connection back to your machine, which firewalls and NAT routers often block. Passive mode makes your script open the data connection to the server instead, which works on most networks. Call ftp_pasv($ftp, true) after logging in.
ftp_connect() opens a plain FTP connection where the username, password, and files travel unencrypted. ftp_ssl_connect() opens an explicit FTP over TLS (FTPS) connection so credentials and data are encrypted. Prefer ftp_ssl_connect() whenever the server supports it. It needs PHP built with OpenSSL.
Use FTP_BINARY for images, archives, PDFs, and any non-text file. Use FTP_ASCII only for plain text where line endings should be converted between systems. When in doubt, FTP_BINARY is the safe choice.
Practice the examples here, then open the FTP Functions index for all 36 function guides, from ftp_alloc() to ftp_systype().