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);
Call
What 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
Parameter
Description
ftp
Required. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
enable
Required. Pass true to enable passive mode or false to use active mode.
Return Value
Return Value
Description
bool
true 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.
Before logging in, the server is not ready to accept the passive command.
The first ftp_pasv() call returns false, and @ hides the warning.
ftp_login() authenticates the user.
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);
Output
Worked in passive mode
How It Works
The loop tries passive mode first (true), then active mode (false).
ftp_pasv() sets the mode before each listing attempt.
As soon as ftp_nlist() succeeds, the script reports which mode worked and stops.
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.