尝试使用php中的ssh2_scp_recv将目录的所有内容从一台服务器获取到另一台服务器


Trying to get all content of directory from one server to another using ssh2_scp_recv in php

下面的脚本正在成功运行。

从一台服务器到另一台获取文件

if(ssh2_scp_recv($conn, '/var/www/html/captures/store/2016/04/HK/15721022890870/test/vcredist.bmp', 
    '/var/www/html/captures/store/2016/04/HK/15721022890870/test/vcredist.bmp')){
echo "'n recevied 'n";
  }else{
 echo "'n not recevied'n";
  }

但为了获取一个静态文件,我想获取包含所有内容的文件夹。

在上面的例子中,我想获取到本地服务器的目录是"15721022890870"

/var/www/html/captures/store/2016/04/HK/15721022890870/

我尝试了以下代码,但不起作用,

远程服务器有目录,但本地服务器没有,所以我想制作目录,然后将其所有内容复制到中

if(ssh2_scp_recv($conn, '/var/www/html/captures/store/2016/04/HK/15721022890870/', 
    '/var/www/html/captures/store/2016/04/HK/')){
echo "'n recevied done 'n";
  }else{
  echo "'n not done 'n";
   }
<?php
$username = "your_username";
$password = "your_pass";
$url      = 'your_stp_server_url';
// Make our connection
$connection = ssh2_connect($url);
// Authenticate
if (!ssh2_auth_password($connection, $username, $password)) throw new Exception('Unable to connect.');
// Create our SFTP resource
if (!$sftp = ssh2_sftp($connection)) throw new Exception('Unable to create SFTP connection.');
$localDir  = '/path/to/your/local/dir';
$remoteDir = '/path/to/your/remote/dir';
// download all the files
$files    = scandir('ssh2.sftp://' . $sftp . $remoteDir);
if (!empty($files)) {
  foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
      ssh2_scp_recv($connection, "$remoteDir/$file", "$localDir/$file");
    }
  }
}
?>

这是从服务器到计算机的,但您可以修改$localDir