使用phpseclib Net_SFTP.get下载文件夹不起作用

使用phpseclib Net_SFTP.get下载文件夹不起作用,php,sftp,phpseclib,Php,Sftp,Phpseclib,我正在尝试使用phpseclib访问SFTP文件夹服务器中的文件。但是当我尝试使用$sftp->get时,它返回false。我根本不知道如何调试这个问题 public function get_file_from_ftps_server() { $sftp = new \phpseclib\Net\SFTP(getenv('INSTRUM_SERVER')); if (!$sftp->login(getenv('INSTRUM_USERNAME'), getenv('INS

我正在尝试使用phpseclib访问SFTP文件夹服务器中的文件。但是当我尝试使用
$sftp->get
时,它返回
false
。我根本不知道如何调试这个问题

public function get_file_from_ftps_server()
{
    $sftp = new \phpseclib\Net\SFTP(getenv('INSTRUM_SERVER'));
    if (!$sftp->login(getenv('INSTRUM_USERNAME'), getenv('INSTRUM_PASSWORD'))) {
        exit('Login Failed');
    }

    $this->load->helper('file');           
    $root  = dirname(dirname(__FILE__));
    $root .= '/third_party/collections_get/';
    $path_to_server = 'testdownload/';
    $result = $sftp->get($path_to_server, $root);

    var_dump($result);
}
$result
中,我得到了一个
false
,我不确定为什么会发生这种情况,我阅读了他们的文档,但仍然不确定。Root是我希望存储信息的目录。现在,我只在那里添加了一个trial.xml文件,但我想知道如果文件夹中有多个文件,如何才能获得多个文件

以下是服务器结构的图片:


使用
Net\u SFTP.get
方法只能下载单个文件。您不能使用它下载整个目录


如果您想下载整个目录,您必须使用“列表”方法之一(
Net\u SFTP.nlist
Net\u SFTP.rawlist
)来检索文件列表,然后逐个下载文件。

通常,当我使用
SFTP
时,我通常会更改目录,然后尝试下载信息

$sftp->pwd(); // This will show you are in the root after connection.
$sftp->chdir('./testdownload'); // this will go inside the test directory.
$get_path = $sftp->pwd()
//If you want to download multiple data, use
$x = $sftp->nlist();
//Loop through `x` and then download the file using.
$result = $sftp->get($get_path); // Normally I use the string information that is returned and then download using

file_put_contents($root, $result);

// Root is your directory, and result is the string.


一开始我甚至无法下载一个文件。之后我可以尝试列表方法,首先我需要得到1个文件。但是你的代码不能工作。不能将文件夹的路径指定为
$remote\u file
$local\u file
。您必须使用文件的路径:
$sftp->get(“/testdownload/trial.xml”,“/third\u party/collections\u get/trial.xml”)“不工作”不作为问题描述工作。给我们看一个日志文件。嗨,Martin,很抱歉回复太晚了,但是当我尝试var_dump($result)时,我只得到true或false,我如何向您显示日志文件?我对代码进行了编辑,正如您所说,它只返回一个false in结果。如果您想下载带有phpseclib的文件夹,请尝试@neubert中描述的技术,这是一个完整的文件夹,为什么不仅仅是文件?正如Martin Prikryl指出的,你的帖子听起来像是在问一个文件夹。不管怎样,看起来Martin Prikryl的思路是正确的。如何将文件下载到本地?您不只是将文件内容从$get\u path写入字符串$result,然后将其写入路径$root中的文件吗?您的
文件内容如何工作?是不是
($fileName,$result)
<?php
use phpseclib\Net\SFTP;

$sftp = new SFTP("server");

if(!$sftp->login("username", "password")) {
    throw new Exception("Connection failed");
}

// The directory you want to download the contents of
$sftp->chdir("/remote/system/path/");

// Loop through each file and download
foreach($sftp->nlist() as $file) {
    if($file != "." && $file != "..")
        $sftp->get("/remote/system/path/$file", "/local/system/path/$file");
}
?>