What are some common reasons for the failure of the ssh2_scp_send function in PHP when copying files to an SFTP server?

Common reasons for the failure of the ssh2_scp_send function in PHP when copying files to an SFTP server include incorrect file paths, insufficient permissions, or issues with the SSH connection. To solve this issue, ensure that the file paths are correct, the server has the necessary permissions to write the file, and the SSH connection is properly established.

$local_file = '/path/to/local/file.txt';
$remote_file = '/path/to/remote/file.txt';

$connection = ssh2_connect('sftp.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

if (ssh2_scp_send($connection, $local_file, $remote_file)) {
    echo "File successfully copied to SFTP server.";
} else {
    echo "Failed to copy file to SFTP server.";
}