[ACCEPTED]-Check remote directory using PHP SSH2-ssh

Accepted answer
Score: 23

You can use file_exists using sftp prefixing 2 'ssh2.sftp://'

For example, with an stablished 1 connection you can:

$sftp = ssh2_sftp($connection);
$fileExists = file_exists('ssh2.sftp://' . $sftp . '/home/marco');
Score: 3

I would recommend abandoning PHP SSH2 in 13 lieu of phpseclib, a pure PHP SSH implementation.

Among other things, PHP SSH2's 12 API sucks. Private keys have to be saved 11 on the filesystem to be loaded whereas with 10 phpseclib all they need be is strings. You 9 can take a key from $_POST without having 8 to dump it to the filesystem as libssh2 7 requires. To top it off, libssh2 requires 6 you have a separate file for the publickey, which 5 is brain dead, since the private key contains the 4 public key.

ssh2_exec(), from libssh2, also 3 returns ANSI color codes and sometimes never 2 returns output and sometimes does (it's 1 inconsistent).

Finally, phpseclib is just plain faster.

Score: 1

assuming is a linux server

$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$cmd = 'if test -d "/YOUR_DIRECTORY"; then echo 1; fi';
$stream = ssh2_exec($connection, $cmd);


Score: 0
 $connection = ssh2_connect('shell.example.com', 22);
 ssh2_auth_password($connection, 'username', 'password');

 $sftp = ssh2_sftp($connection);

 $stream = file_exists("ssh2.sftp://$sftp/path/to/file");


More Related questions