Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 modeFALSE− 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(), orftp_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.
