代码之家  ›  专栏  ›  技术社区  ›  Masnad Nihit

使用phpseclib Net\u SFTP下载文件夹。get不起作用

  •  4
  • Masnad Nihit  · 技术社区  · 7 年前

    我正在尝试使用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 ,我得到一个 我不知道为什么会这样,我读了他们的文档,但仍然不确定。Root是我希望存储信息的目录。现在我只添加了一个试用版。xml文件,但也不知道如何获得多个文件,如果它在文件夹中。

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

    structure

    4 回复  |  直到 6 年前
        1
  •  3
  •   XAF    7 年前

    通常当我使用 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.
    
        2
  •  2
  •   Martin Prikryl    7 年前

    这个 Net_SFTP.get 方法只能下载单个文件。您不能使用它下载整个目录。

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

        3
  •  0
  •   c0nfus3d    4 年前
    <?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");
    }
    ?>
    
        4
  •  0
  •   Frontliner    3 年前

    虽然我有点晚了,但我还是想和大家分享这一点。 我采用的方法是使用zip文件下载文件夹。这样做的原因是,你会收到一个反馈,说有东西正在下载。

    如果你不想这样,只需删除与zip相关的内容。然后,拆下收割台并用 $sftp->get("remote/file", "local/file");

    <?PHP
    use phpseclib\Net\SFTP;
    
    $sftp = new SFTP("IP:Port");
    
    if(!$sftp->login("username", "password")) throw new Exception("Connection failed");
    
    # Create directory variable
    $directory =  "/remote/path/";
    
    # Set directory
    $sftp->chdir($directory);
    
    # File Name
    $name =  'file';
    
    # Retrieve file
    $file = $sftp->get('file');
    
    # Check if is folder
    if ($sftp->is_dir($file)) {
    
      # Temporarily file
      $tmp = sys_get_temp_dir()."\\yourSite_".rand().".zip";
    
      # Create new Zip Archive.
      $zip = new ZipArchive();  
    
      # Open new Zip file
      $zip->open($tmp,  ZipArchive::CREATE | ZipArchive::OVERWRITE);
    
      function recursive($src, $zip, $sftp) {
    
        # Loop Through files
        foreach ($sftp->nlist($src) as $file) {
    
         # Skip . & ..
         if ($file == "." || $file == "..") continue;
    
         if (!$sftp->is_file($src . "/" . $file)) {
    
              # Make directory
              $zip->addEmptyDir($src . "/" . $file);
    
              # Run the loop again
              recursive($src . "/" . $file, $zip, $sftp);
    
            } else {
              # Add file to zip within folder
              $zip->addFromString($src . "/" . $file,  $sftp->get($src . "/" . $file));
    
          }
    
        }
    
      }
    
      # Run Recursive loop
      recursive($name, $zip, $sftp);
    
      # Close zip file
      $zip->close();
    
      header('Content-Description', 'File Transfer');
      header('Content-type', 'application/zip');
      header('Content-Disposition', 'attachment; filename="' . $name . '.zip"');
      header('Content-length', filesize($tmp));
    
      echo file_get_contents($tmp);
    
      # Delete temporarily file
      unlink($tmp);
      return;
    
    }
    
    # Otherwise download single file
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=\"". $name ."\"");
    
    echo $file;
    return;