What are the differences between FTP over SSH and SFTP in terms of PHP usage?

FTP over SSH and SFTP both provide secure file transfer mechanisms, but they operate in different ways. FTP over SSH uses the FTP protocol over a secure SSH connection, while SFTP is a separate protocol that runs over SSH. In terms of PHP usage, SFTP is generally preferred as it is more secure and easier to implement compared to FTP over SSH.

// Example of using SFTP in PHP
$connection = ssh2_connect('sftp.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection);

$stream = fopen("ssh2.sftp://$sftp/path/to/remote/file", 'r');

// Read the content of the file
$contents = stream_get_contents($stream);

fclose($stream);