PHP ftp_pasv() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_pasv() turns passive mode on or off for an FTP connection. In passive mode, your PHP script opens the data connection to the server instead of the server connecting back to you.

This matters because most home networks and hosting firewalls block incoming connections. If uploads, downloads or directory listings hang or fail, switching to passive mode is usually the fix.

Syntax

Call ftp_pasv() with the values you need. Start with this line.

PHP
ftp_pasv($ftp, true);
CallWhat it does
ftp_pasv($ftp, true)Turns passive mode on. Returns true on success.
ftp_pasv($ftp, false)Switches back to active mode.
if (!ftp_pasv($ftp, true)) { ... }Checks that the server accepted passive mode.

You must call ftp_pasv() after ftp_login(), never before. Passive mode is off by default in PHP, so most scripts that talk to servers behind firewalls should turn it on right after logging in.

Library declarations
PHP
ftp_pasv(FTP\Connection $ftp, bool $enable): bool

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
enableRequired. Pass true to enable passive mode or false to use active mode.

Return Value

Return ValueDescription
booltrue on success, or false on failure.

PHP Examples

Example 1 — Enable Passive Mode After Login

Log in, then turn on passive mode before listing files.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

if (ftp_pasv($ftp, true)) {
    echo "Passive mode is on", PHP_EOL;
}

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

ftp_close($ftp);

How It Works

  1. ftp_login() must succeed first, so the server accepts the command.
  2. ftp_pasv() with true asks the server to switch to passive mode.
  3. The if statement confirms that the server agreed.
  4. ftp_nlist() now opens its data connection in passive mode and returns the list.

Example 2 — Wrong Order Fails

Calling ftp_pasv() before ftp_login() does not work. Always log in first.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');

$before = @ftp_pasv($ftp, true);
echo "Before login: ", var_export($before, true), PHP_EOL;

ftp_login($ftp, 'username', 'password');

$after = ftp_pasv($ftp, true);
echo "After login: ", var_export($after, true), PHP_EOL;

ftp_close($ftp);

How It Works

  1. Before logging in, the server is not ready to accept the passive command.
  2. The first ftp_pasv() call returns false, and @ hides the warning.
  3. ftp_login() authenticates the user.
  4. The second ftp_pasv() call works and returns true.

Example 3 — Retry in Active Mode if Passive Fails

Some old servers do not support passive mode. Try it first, then fall back to active mode when needed.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

$files = false;

foreach ([true, false] as $passive) {
    ftp_pasv($ftp, $passive);
    $files = @ftp_nlist($ftp, '.');

    if ($files !== false) {
        echo $passive ? "Worked in passive mode" : "Worked in active mode", PHP_EOL;
        break;
    }
}

if ($files === false) {
    echo "Could not list files in either mode", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The loop tries passive mode first (true), then active mode (false).
  2. ftp_pasv() sets the mode before each listing attempt.
  3. As soon as ftp_nlist() succeeds, the script reports which mode worked and stops.
  4. If both modes fail, a final message tells the user.

Frequently Asked Questions

It switches an FTP connection between passive and active mode. In passive mode, your script opens the data connection to the server, which works well through firewalls.
Use it whenever you are behind a firewall or router, which is almost always. It is also the fix when downloads, uploads or listings hang.
Most often because you called it before ftp_login(), or because the server does not support passive mode.
No. PHP starts in active mode, so you need to call ftp_pasv($ftp, true) after logging in.
Not by itself. It only changes how the data connection is opened. For security, use ftp_ssl_connect() or an SFTP library.

Next: ftp_put()

ftp_put() uploads a file from your own computer or server to a remote FTP server. It is th.

ftp_put() tutorial →

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