Skip to content

Instantly share code, notes, and snippets.

@edsonhoraciojunior
Created November 3, 2016 19:50
Show Gist options
  • Save edsonhoraciojunior/5641d2299a6a38f767e12fa3c34870f5 to your computer and use it in GitHub Desktop.
Save edsonhoraciojunior/5641d2299a6a38f767e12fa3c34870f5 to your computer and use it in GitHub Desktop.
Download remote files using PHP's ssh2 and ssh2_scp
<?php
// Just for logging
$begin = microtime(true);
$totalSize = 0;
$totalQty = 0;
$totalNotFoundQty = 0;
// Remove script running time limit
set_time_limit(0);
// Change this to your preference, files will be download from here
$remotePath = '/remote/path/to/files/';
// Change this to your preference, files will be downloaded here
$localPath = '/home/username/Downloads/remote-fotos/';
/**
* list of remote files you want to download
* there's a lot of ways you can get this, I personally got from database and iterated through it
* but I won't get this part into details because it's not the objective of this script
*/
$remoteFileNames = array(
'file-name-1',
'file-name-2',
'file-name-3',
);
try {
// change these to your preference
$hostname = 'example_ip';
$username = 'username';
$publicKey = '/home/username/.ssh/id_rsa.pub';
$privateKey = '/home/username/.ssh/id_rsa';
/**
* connect via ssh
*
* There's a known bug with ssh2_connect when your private key use passphrase, in that case, unencrypt your
* private key and use that new file
* more details here: http://stackoverflow.com/a/12036066/2312615
*/
$resourceConn = ssh2_connect($hostname);
if (!$resourceConn) {
throw new \Exception('Didn\'t connect');
}
// authenticate
$authenticated = ssh2_auth_pubkey_file($resourceConn, $username, $publicKey, $privateKey);
if (!$authenticated) {
throw new \Exception('Didn\'t authenticate');
}
// Download one file per iteration
foreach ($remoteFileNames as $fileName) {
// creates destiny path if it doesn't exists
if (!is_dir($localPath)) {
if (!mkdir($localPath)) {
throw new \Exception('Could not create destiny folder');
}
}
$remoteFullPath = $remotePath.$fileName;
$localFullPath = $localPath.$fileName;
// Download file from $remoteFullPath to $localFullPath
$success = ssh2_scp_recv($resourceConn, $remoteFullPath, $localFullPath);
if (!$success) {
printf('Couldn\'t download file %s'.PHP_EOL, $fileName);
$totalNotFoundQty++;
} elseif (file_exists($localPathFile)) {
$totalSize += filesize($localPathFile);
$totalQty ++;
}
// Prints a log message every 100 files just to know what's going on
if ($totalQty % 100 == 0) {
printf('Downloaded %s files so far'.PHP_EOL, $totalQty);
}
}
// close ssh connection
if ($resourceConn !== false) {
unset($resourceConn);
}
} catch (\Exception $exc) {
echo "Error: ".$exc->getMessage().PHP_EOL;
// conexão ssh
if (isset($resourceConn)) {
unset($resourceConn);
}
exit;
}
$end = microtime(true);
printf("Total files: %d, size: %s MB, not found: %d, script execution time: %ss".PHP_EOL,
$totalQty,
number_format($totalSize/1024/1024, 2, ',', '.'),
$totalNotFoundQty,
number_format($end - $begin, 2, ',', '.')
);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment