0

We are attempting to move only the files from a remote server and putting the files directories into our database, so we need to be able to distinguish between a file and a directory. We have successfully been able to connect via SSH2 and we are able to read and display the files and directories within the remote path top directory. However, we have not been able to locate a php script that will allow us to find whether the returned name is a directory or a file. Below are a few examples we have tried. Any help is greatly appreciated and we thank you in advance.

$connection = ssh2_connect('www.myremote.com', 22);
ssh2_auth_password($connection, 'user', 'pw');

$sftp = ssh2_sftp($connection);

// THIS WORKS NICELY TO DISPLAY NAME FROM REMOTE SERVER
$handle = opendir("ssh2.sftp://$sftp/remotepath/");
echo "Directory handle: $handle<br>";
echo "Entries:<br>";
while (false != ($entry = readdir($handle))){
    echo "$entry<br>";
}

// ONE OPTION THAT DOES NOT WORK
$handle = opendir("ssh2.sftp://$sftp/remotepath/");
echo "<br><br>2Directory handle: $handle<br>";
echo "4Entries:<br>";
while (false != ($entry = readdir($handle))){
    if (is_dir(readdir($handel.''.$entry) ) ) {echo "<strong>$entry</strong><br>"; } else {
    echo "$entry<br>"; //}
}

// ANOTHER OPTION THAT RETURNS AN EMPTY RESULT
$files = scandir('ssh2.sftp://'.$sftp.'/remotepath/');
foreach ($files as $file):
    if (is_dir('ssh2.sftp://'.$sftp.'/remotepath/'.$file) ) {"<strong>".$file."</strong><br>"; } else { $file.'<br>'; } 
endforeach;
ggSmith
  • 5
  • 3

1 Answers1

0

if you use the Linux command line find this will help:

this is how you find only files named blah:

find . -type f -name *blah*

this is how you find only directories named blah:

find . -type d -name *blah*

in this case you could to the following to find all files in the /tmp directory (without going into the subdirectories of /tmp (maxdepth 1)) named anything:

$connection->exec('find /tmp -maxdepth 1 -type f -name "*"');

EDIT:

Ok, so here's a bit more code. This will connect to a server and echo a list of all directories in your home directory and then all files in your home directory:

$connection = ssh2_connect($host, 22);
ssh2_auth_password($connection, $user, $pass);

$the_stream = ssh2_exec($connection, '/usr/bin/find ~ -maxdepth 1 -type d');
stream_set_blocking($the_stream, true);
$the_result = stream_get_contents($the_stream);
echo "Directories only: <br><pre>" . $the_result . "</pre>";
fclose($the_stream);

$the_stream = ssh2_exec($connection, '/usr/bin/find ~ -maxdepth 1 -type f');
stream_set_blocking($the_stream, true);
$the_result = stream_get_contents($the_stream);
echo "Files only: <br><pre>" . $the_result . "</pre>";
fclose($the_stream);

You should be able to parse $the_result into an array by splitting on newlines or some such and get just the files or just the directories. Remove the "-maxdepth 1" from the find and you'll recurse through all subdirectories.

Matt Pavelle
  • 819
  • 5
  • 8
  • Matt, thank you for your quick response. I am not following how to integrate your idea into the php script. Here is what I tried and it still returns both directories and files the same. $files = scandir('ssh2.sftp://'.$sftp.'/remotepath/'); foreach ($files as $file): echo $file.'-3333
    '; if (ssh2_exec($connection, 'find /remotepath/'.$file.' -maxdepth 1 -type d -name "*"') ) {"".$file."
    "; } else { $file.'-344
    '; } endforeach;
    – ggSmith Aug 02 '13 at 23:34
  • I am trying it now and let you know my results. Thanks for the more detail. – ggSmith Aug 03 '13 at 01:28
  • Matt, thank you for the examples. It has worked very nicely and we have ben able to pull the directories from the files. Thank you again. – ggSmith Aug 03 '13 at 01:56