-
-
Save danielbwa/6993444 to your computer and use it in GitHub Desktop.
<?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"); | |
} | |
} | |
} | |
?> |
thanks
Great !
What if i just need to list directory rather than downloading !
Downloading takes huge time !
Pls let me know.
thanks
@cbvora just echo $file
in the foreach loop to list the files in the given dir.
Please replay me ASAP
Hello, the scipt is not iterating over the loop, pausing at the 1st file it gets from the files lists. and the 1st file which is copied also has no content inside it.
Can this be modified to download a X number of files instead of all the files???
@belgareth That should be easy to do, not sure why you want to do that but keep a count in the foreach and break out of the foreach with break; when your desired number of download has been reached. http://php.net/manual/en/control-structures.break.php
Just what I needed. A remote download via SSH
Suddenly, it doesn't work for files with tab in the name
Works great!