1

The below script is working successfully.

Getting file from one server to another

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

But instead for fetching just a static file, I want to fetch folder with all its content inside.

With above example, the directory I would like to fetch to local server is "15721022890870"

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

I have tried below code but doesn't work,

The remote server has directory, but the local server doesn't have, so I want to make directory then copy all its content inside

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";
   }
sanainfotech
  • 571
  • 4
  • 11
  • 29
  • `ssh2_scp_recv` does not support multiple file copies. You might be successful with `ssh2_exec` and/or `ssh2_tunnel`, but just an unproved idea. What is your environment? Don't you have access to the system via `exec`? Generally, why are you using PHP? – Pinke Helga May 10 '16 at 09:06
  • All server is based on Ubuntu, Linux. Yes, I have full access system via exec. The reason I am using PHP there is post-processing script before fetching content – sanainfotech May 10 '16 at 09:21
  • So why don't you call `scp` via exec? When using key pair, no interaction is required. – Pinke Helga May 10 '16 at 10:02

1 Answers1

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");
    }
  }
}
?>

This is from server to computer but you can modify the $localDir

Belgarath
  • 104
  • 12