I would like to list the files of a directory in an other server
I am connected to an other server using ssh2_connect function the connection is going well and I am able to fetch a desired file but I am not sure how the files can be listed.
You can use ssh2_sftp and opendir, like this:
<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$sftp = ssh2_sftp($connection);
$sftp_fd = intval($sftp);
$handle = opendir("ssh2.sftp://$sftp_fd/path/to/directory");
echo "Directory handle: $handle\n";
echo "Entries:\n";
while (false != ($entry = readdir($handle))){
echo "$entry\n";
}
$sftp
resource returned by ssh2_sftp
through a intval
first, before concatenating it with anything, as stated by @pcs' answer.
Jan 29, 2017 at 17:41
/./
as the path, eg. opendir("ssh2.sftp://$sftp_fd/./")
. I struggled with it for some time because there was no error message and I found an answer in another thread: stackoverflow.com/a/16238476/1743367 . Sharing here in case anyone else has a similar problem.
Feb 19, 2020 at 18:40
ssh2_sftp_stat()
can be used to determine file size, timestamps, permissions, ownership etc. The 'mode'
element can be used to distinguish between file/directory/link/etc.
Oct 8, 2020 at 10:27
In case anybody is struggling to get this to work, and you are running PHP 5.6.28
there was a recent update that either created a requirement or introduced a bug where intval()
must be used on each SFTP folder/file access function:
$handle = opendir("ssh2.sftp://".intval($sftp)."/path/to/directory");
closedir($handle);
solves your issue? it works for me.
Here's a method which can scan directories recursively and return multi-dimensional arrays if the recursive parameter is set, or only scan the path and return a single dimension array containing files in that directory if it's not. Can be modified to also include directories without contents in non-recursive mode if needed.
Creating it as a class makes it easy to be reused later. I only included the methods from my class that were required to answer the question.
$host = 'example.com';
$port = 22;
$username = 'user1';
$password = 'password123';
$path = '.';
$recursive = true;
$conn = new SFTP($host, $port);
$conn->login($username, $password);
$files = $conn->ls($path, $recursive);
var_dump($files);
class SFTP
{
private $connection;
private $sftp;
public function __construct($host, $port = 22)
{
$this->connection = @ssh2_connect($host, $port);
if (! $this->connection)
throw new Exception("Could not connect to $host on port $port.");
}
public function login($username, $password)
{
if (! @ssh2_auth_password($this->connection, $username, $password))
throw new Exception("Could not authenticate with username $username");
$this->sftp = @ssh2_sftp($this->connection);
if (! $this->sftp)
throw new Exception("Could not initialize SFTP subsystem.");
}
public function ls($remote_path, $recursive = false)
{
$tmp = $this->sftp;
$sftp = intval($tmp);
$dir = "ssh2.sftp://$sftp/$remote_path";
$contents = array();
$handle = opendir($dir);
while (($file = readdir($handle)) !== false) {
if (substr("$file", 0, 1) != "."){
if (is_dir("$dir/$file")){
if ($recursive) {
$contents[$file] = array();
$contents[$file] = $this->ls("$remote_path/$file", $recursive);
}
} else {
$contents[] = $file;
}
}
}
closedir($handle);
return $contents;
}
}
http://www.php.net/manual/en/function.ssh2-exec.php
You give it the ls
command, assuming it is a UNIX-based system (usually the case), otherwise the OP-specific command like dir
for Windows.
<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'ls');
?>
ls
don't help if you want to loop through the directory entries. Also, it won't work if the host only allows the SFTP subsytem and deny shell access.
Jan 12, 2012 at 19:50