1

I am connecting to remote sftp server using ssh2 functions. Now I need to get current directory name. In case of ftp server I can use ftp_pwd but I can't find similar function for ssh2

I need php code

user3918128
  • 101
  • 1
  • 2
  • 9

3 Answers3

3

Give phpseclib a try for sftp connections, you can do easily get the current working dir with its built-in method pwd

From the directory management examples:

print $sftp->pwd();
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
  • I found this solution $stream = ssh2_exec($connection, 'pwd'); stream_set_blocking($stream, true); $homeDir = stream_get_contents($stream); – user3918128 Dec 09 '15 at 15:15
0

Have you tried only pwd ? Type help for list of available commands

J. Grunder
  • 143
  • 2
  • 13
0

On Ubuntu 20.04 SFTP server ssh2_sftp_realpath returns the same as running pwd interactively

<?php
$connection = ssh2_connect('host.example.com', 22);

ssh2_auth_password($connection, 'sftptest', 'password');

$sftp = ssh2_sftp($connection);

$realpath = ssh2_sftp_realpath($sftp, '.');

# $realpath is now: '/home/sftptest'

$realpath = ssh2_sftp_realpath($sftp, './test');

# $realpath =  /home/sftptest/test

$realpath = ssh2_sftp_realpath($sftp, "../test2");

# $realpath = /home/test2

# note that the directories don't have to exist it just resolves to what it should be based on the current directory
?>