What are the necessary parameters for establishing an FTP connection in PHP?

When establishing an FTP connection in PHP, you need to provide the FTP server address, username, password, and optional port number if it's different from the default FTP port (21). Additionally, you may need to specify the connection mode (active or passive) and set other configuration options like timeout and transfer mode.

// Set up FTP connection parameters
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$ftp_port = 21; // Optional, default is 21
$ftp_mode = FTP_ASCII; // Optional, set transfer mode (ASCII or binary)
$ftp_timeout = 90; // Optional, set timeout in seconds

// Connect to FTP server
$conn_id = ftp_connect($ftp_server, $ftp_port, $ftp_timeout);

// Login to FTP server
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

// Set connection mode
ftp_pasv($conn_id, true); // Set passive mode

// Check if connection was successful
if ($conn_id && $login_result) {
    echo "Connected to $ftp_server";
} else {
    echo "Failed to connect to $ftp_server";
}

// Close FTP connection
ftp_close($conn_id);