What role does the port number play in FTP connections when uploading files in PHP?

When uploading files using FTP connections in PHP, the port number is important as it specifies the network port to be used for the FTP connection. By default, FTP connections use port 21, but in some cases, the FTP server may be configured to use a different port. Therefore, specifying the correct port number in the FTP connection settings is crucial for successful file uploads.

// FTP server details
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$ftp_port = 21; // Specify the correct port number here

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

// Login to FTP server
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// Check if connection is successful
if (!$ftp_conn || !$login) {
    die('FTP connection failed');
}

// Upload file to FTP server
ftp_put($ftp_conn, '/path/to/remote/file.txt', '/path/to/local/file.txt', FTP_ASCII);

// Close FTP connection
ftp_close($ftp_conn);