What potential pitfalls can occur when using the ftp_connect function in PHP for server status checks?

When using the ftp_connect function in PHP for server status checks, potential pitfalls include timeouts due to slow server response times, incorrect server credentials leading to authentication failures, and network connectivity issues causing connection failures. To mitigate these issues, it's important to handle potential errors gracefully by implementing error handling mechanisms such as try-catch blocks and setting appropriate timeout values.

<?php
$server = 'ftp.example.com';
$port = 21;
$username = 'username';
$password = 'password';

$conn = ftp_connect($server, $port, 10); // Set timeout to 10 seconds

if (!$conn) {
    echo 'Failed to connect to the server.';
} else {
    $login = ftp_login($conn, $username, $password);

    if (!$login) {
        echo 'Failed to login to the server.';
    } else {
        echo 'Connected and logged in successfully.';
    }

    ftp_close($conn);
}
?>