Not sure what's wrong but I am unable to send or receive file using SSH.
I am using the following code
define('SSH_HOST', 'HOST');
define('SSH_USER', 'USER');
define('SSH_PASS', 'PASSWORD');
$connection = ssh2_connect(SSH_HOST, 22);
ssh2_auth_password($connection, SSH_USER, SSH_PASS);
$remoteFile = '/remote/absolute/path/file.ext';
$localFile = '/local/absolute/path/file.ext';
if(ssh2_scp_recv($connection, $remoteFile, $localFile)){
echo("received");
}else{
echo("NOT received");
}
Neither this nor file_get_contents
function is working. The strange thing is that I am able to get file stats by calling
$sftp = ssh2_sftp($connection);
$statinfo = ssh2_sftp_stat($sftp, $remoteFile);
But unable to read file data.
Is there some special permission I have to set on either server?
SCP and SFTP are different things. Probably, SCP is disabled on your server.
I would use phpseclib, a pure PHP SFTP implementation . Easier to use and easier to diagnose problems with it due to its logging capabilities. ie.
<?php
include('Net/SFTP.php');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);
$ssh = new Net_SFTP('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}
$ssh->put('filename.ext', 'zzzzzzzzzzzzzzz');
echo $ssh->getLog();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.