Reputation: 77
i am attempting to create a web based sftp page, the only part i am having problems with is trying to determin the the listed file is a file or folder. i am sucessfully listing a directory using
$connection = ssh2_connect($_SESSION['Server'], $_SESSION['Port']);
if(ssh2_auth_password($connection, $_SESSION['Username'], $_SESSION['Password']))
{
$sftp = ssh2_sftp($connection);
$dir = "ssh2.sftp://$sftp/$directory";
$handle = opendir($dir);
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$temp=$file;
if(strlen($directory) > 0)
$Path=$directory."/".$file;
else
$Path=$file;
$statinfo = ssh2_sftp_stat($sftp, "/".$Path);
echo "<a href=\"list.php?dir=".urlencode($Path)."\">$temp</a><br />";
}
}
closedir($handle);
}
i attempted to use the is_dir() and is_file() php function on $file which works sucessfully when working on local filesystem, but when using SFTP both come back FALSE, i attempted to use ssh2_sftp_stat() but this only gives uid, gid, size access and modified times and i dont think there is anyting that can be used to identify files of folders, if anyone has a way to resolve this i would appreaciate it greatly
Vip32
Upvotes: 0
Views: 4956
Reputation: 5213
I would strongly suggest using phpseclib for SSH2 purposes. It greatly simplifies tasks like this.
Solution for your problem using phpseclib should go something like this:
include('Net/SFTP.php');
$sftp = new Net_SFTP($_SESSION['Server'] . ':' . $_SESSION['Port']);
if (!$sftp->login($_SESSION['Username'], $_SESSION['Password'])) {
exit('Login Failed');
}
foreach($sftp->rawlist($dir) as $filename => $attrs) {
if ($attr['type'] == NET_SFTP_TYPE_REGULAR) {
echo $filename . ' is a regular file!';
}
if ($attr['type'] == NET_SFTP_TYPE_DIRECTORY) {
echo $filename . ' is a directory!';
}
}
These are the file type constants phpseclib uses:
file_types = array(
1 => 'NET_SFTP_TYPE_REGULAR',
2 => 'NET_SFTP_TYPE_DIRECTORY',
3 => 'NET_SFTP_TYPE_SYMLINK',
4 => 'NET_SFTP_TYPE_SPECIAL'
);
Upvotes: 5