1

下面的脚本运行成功。

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

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";
   }
4

1 回答 1

0
<?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

于 2017-06-05T10:39:36.833 回答