简体   繁体   English

php sftp文件或文件夹

[英]php sftp file or folder

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. 我正在尝试创建一个基于Web的sftp页面,我遇到的唯一问题是试图确定列出的文件是文件还是文件夹。 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 我试图在$ file上使用is_dir()和is_file()php函数,当在本地文件系统上工作时,该函数成功运行,但是当同时使用SFTP都返回FALSE时,我试图使用ssh2_sftp_stat(),但这仅给出了uid,gid,大小访问和修改的时间,我不认为有任何可用于标识文件夹文件的方法,如果有人有办法解决这个问题,我将大为赞赏

Vip32 Vip32

I would strongly suggest using phpseclib for SSH2 purposes. 我强烈建议您将phpseclib用于SSH2。 It greatly simplifies tasks like this. 它极大地简化了这样的任务。

Solution for your problem using phpseclib should go something like this: 使用phpseclib解决问题的方法应该是这样的:

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: 这些是phpseclib使用的文件类型常量:

file_types = array(
    1 => 'NET_SFTP_TYPE_REGULAR',
    2 => 'NET_SFTP_TYPE_DIRECTORY',
    3 => 'NET_SFTP_TYPE_SYMLINK',
    4 => 'NET_SFTP_TYPE_SPECIAL'
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM