ftp_pasv() function in PHP

The ftp_pasv() function in PHP enables or disables passive mode for FTP connections. Passive mode allows the client to initiate data connections, which is often required when working behind firewalls or NAT routers.

Syntax

ftp_pasv(connection, pasv)

Parameters

  • connection − The FTP connection resource returned by ftp_connect()

  • pasv − A boolean value that specifies the passive mode state −

    • TRUE − Enable passive mode

    • FALSE − Disable passive mode

Return Value

The ftp_pasv() function returns TRUE on success or FALSE on failure.

Example

The following example demonstrates how to enable passive mode before uploading a file ?

<?php
   $ftp_server = "192.168.0.4";
   $ftp_user = "tim";
   $ftp_pass = "wthbn#@121";
   
   // Establish FTP connection
   $con = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
   $login = ftp_login($con, $ftp_user, $ftp_pass);
   
   // Enable passive mode
   if (ftp_pasv($con, true)) {
      echo "Passive mode enabled<br>";
   } else {
      echo "Failed to enable passive mode<br>";
   }
   
   $local_file = "localfile.txt";
   $server_file = "serverfile.txt";
   
   // Upload file using passive mode
   if (ftp_put($con, $server_file, $local_file, FTP_ASCII)) {
      echo "File uploaded successfully!";
   } else {
      echo "Error uploading file!";
   }
   
   // Close connection
   ftp_close($con);
?>

Key Points

  • Passive mode must be enabled after establishing the FTP connection but before data transfer operations

  • Most modern FTP servers require passive mode for connections behind firewalls

  • The function should be called before any file transfer operations like ftp_get(), ftp_put(), or ftp_nlist()

Conclusion

The ftp_pasv() function is essential for FTP operations in environments with firewalls or NAT. Always enable passive mode before performing file transfers to ensure reliable connections.

Updated on: 2026-03-15T07:41:22+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements