What are common errors encountered when using PHP to establish an FTP connection and how can they be resolved?

Common errors when establishing an FTP connection in PHP include incorrect credentials, passive mode not being enabled, or the FTP extension not being installed. To resolve these issues, double-check the FTP server credentials, enable passive mode using `ftp_pasv()`, and ensure that the FTP extension is enabled in your PHP configuration.

// FTP server credentials
$ftp_server = 'ftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';

// Connect to FTP server
$conn_id = ftp_connect($ftp_server);
if(!$conn_id) {
    die('Unable to connect to FTP server');
}

// Login to FTP server
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
if(!$login_result) {
    die('Login failed');
}

// Enable passive mode
ftp_pasv($conn_id, true);

// Now you can perform FTP operations
// For example: ftp_put(), ftp_get(), ftp_nlist(), etc.

// Close FTP connection
ftp_close($conn_id);