How can one troubleshoot issues related to sftp connectivity in PHP scripts?

To troubleshoot issues related to sftp connectivity in PHP scripts, ensure that the necessary PHP extensions (such as ssh2) are installed and enabled on the server. Check that the correct sftp server address, username, password, and port are being used in the connection settings. Verify that the server's firewall settings allow connections on the specified port.

<?php
$server = 'sftp.example.com';
$username = 'your_username';
$password = 'your_password';
$port = 22;

$conn = ssh2_connect($server, $port);
if (ssh2_auth_password($conn, $username, $password)) {
    echo "Connected to sftp server successfully.";
} else {
    echo "Failed to connect to sftp server.";
}
?>