What is the purpose of using ftp_connect in PHP and what are the common issues associated with it?

When using ftp_connect in PHP, one common issue is that the function may fail to establish a connection to the FTP server due to incorrect server credentials or network issues. To solve this issue, ensure that the FTP server credentials are correct and that the server is reachable from the PHP script's environment.

// 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) {
    // Login to FTP server
    $login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
    
    if ($login_result) {
        echo 'Connected to FTP server and logged in successfully.';
    } else {
        echo 'Failed to login to FTP server. Check credentials.';
    }
} else {
    echo 'Failed to connect to FTP server. Check server address and network connection.';
}