PHP FTP

Beginner
7 min read
Updated: Nov 2026
3 examples
Connect, transfer & secure

Definition and Usage

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.

Every session follows the same shape: ftp_connect() opens the link, ftp_login() signs in, ftp_pasv() prepares data transfers, and then calls such as ftp_put() and ftp_get() upload and download files. ftp_close() ends the session.

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.

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

Core Ideas

What to remember when you use FTP from PHP:

IdeaExampleRemember
Connectftp_connect($host, 21, 30)Returns false on failure; the third argument is the timeout in seconds
Log inftp_login($ftp, $user, $pass)Required before any other command; returns true or false
Passive modeftp_pasv($ftp, true)Call it after login; avoids firewall and NAT trouble
Uploadftp_put(), ftp_fput(), ftp_append()Arguments are remote name first, then local file
Downloadftp_get(), ftp_fget()Arguments are local name first, then remote file
Transfer modeFTP_BINARY, FTP_ASCIIUse binary for everything except plain text
Directoriesftp_pwd(), ftp_chdir(), ftp_mkdir(), ftp_nlist()Work on the remote server, not on your own disk
SSL vs plainftp_ssl_connect() vs ftp_connect()SSL encrypts login and data; plain does not

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.

PHP
<?php
$ftp = ftp_connect('ftp.example.com', 21, 30);
if ($ftp === false) {
    exit("Could not connect\n");
}

if (!ftp_login($ftp, 'demo', 'secret')) {
    ftp_close($ftp);
    exit("Login failed\n");
}

ftp_pasv($ftp, true);

echo 'Connected. Directory: ' . ftp_pwd($ftp) . "\n";
ftp_close($ftp);
?>

How It Works

  1. ftp_connect() opens the control connection and returns false if the server cannot be reached within 30 seconds.
  2. ftp_login() sends the username and password; always check its result before doing more.
  3. ftp_pasv() turns on passive mode so later transfers and listings work behind firewalls.
  4. ftp_pwd() returns the current remote directory, which is often / right after login.

Example 2 — Upload and download a file

Send a local file to the server with ftp_put(), then fetch it back under a new name with ftp_get().

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');
ftp_pasv($ftp, true);

// Upload: remote name first, local file second
if (ftp_put($ftp, 'reports/sales.csv', __DIR__ . '/sales.csv', FTP_BINARY)) {
    echo "Upload OK\n";
}

// Download: local name first, remote file second
if (ftp_get($ftp, __DIR__ . '/sales-copy.csv', 'reports/sales.csv', FTP_BINARY)) {
    echo "Download OK\n";
}

echo 'Size on server: ' . ftp_size($ftp, 'reports/sales.csv') . " bytes\n";
ftp_close($ftp);
?>

How It Works

  1. ftp_put() uploads a local file. The remote path comes first and the local path second.
  2. ftp_get() downloads a remote file. The local path comes first and the remote path second, the reverse of ftp_put().
  3. FTP_BINARY copies bytes unchanged, which is right for CSV files, images, and archives.
  4. ftp_size() returns the remote file size in bytes, or -1 when the server cannot tell.

Example 3 — Work with directories over SSL

Connect with ftp_ssl_connect(), create a folder, move into it, list its content, and clean up.

PHP
<?php
// FTP over TLS: login and data are encrypted
$ftp = ftp_ssl_connect('ftp.example.com');
ftp_login($ftp, 'demo', 'secret');
ftp_pasv($ftp, true);

ftp_mkdir($ftp, 'backups');
ftp_chdir($ftp, 'backups');
echo 'Now in: ' . ftp_pwd($ftp) . "\n";

ftp_put($ftp, 'notes.txt', __DIR__ . '/notes.txt', FTP_ASCII);

print_r(ftp_nlist($ftp, '.'));

ftp_delete($ftp, 'notes.txt');
ftp_cdup($ftp);
ftp_rmdir($ftp, 'backups');
ftp_close($ftp);
?>

How It Works

  1. ftp_ssl_connect() works like ftp_connect() but starts a TLS-encrypted session. The rest of your code does not change.
  2. ftp_mkdir() and ftp_chdir() create a remote folder and make it the working directory; ftp_cdup() goes back up one level.
  3. ftp_nlist() returns an array of file names in a directory, and FTP_ASCII is fine for the small text file used here.
  4. ftp_delete() removes a file and ftp_rmdir() removes an empty folder, so delete the contents first.

Next: PHP FTP Functions Index

After you know the connect, transfer, and directory flow, open the function index for all 36 guides.

Start withWhy
ftp_connect()Open a connection to an FTP server
ftp_login()Sign in with a username and password
ftp_pasv()Turn passive mode on or off
ftp_put()Upload a local file
ftp_get()Download a remote file
ftp_ssl_connect()Connect with FTP over TLS

Full list: PHP FTP Functions index (/php/ftp/function).

Frequently Asked Questions

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().

Browse every FTP function

Open the functions index for categorized guides with examples and FAQs.

FTP Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful