Download PHP from a remote server via sftp

It may have been asked before, I'm new to PHP, and I try to learn as much as possible, but it really left me alone.

Basically, I want to know how I would use the PHP code to make it load everything from the remote server to the local one. This makes it load all of not just one file I'm stuck on. So please, can someone show / explain to me how I will do this?

Thanks in advance.

What i still have

<?php $connection - ssh2_connect('example.com', 22); ssh2_auth_password($connection, 'username', 'password'); $remote_dir="/remote_dir/"; $local_dir="/local_dir/"; $remote ="$remote_dir"; $stream = ssh2_exec($connection, $remote); stream_set_blocking($stream,true); $command=fread($stream,4096); $array=explode(\n,$command); $total_files=sizeof($array); for($i=0;$i<$total_files;$i+++){ $file_name=trim($array[$i]); if($file_name!=''{ $remote_file=$remote_dir.$file_name; $local_file=$local_dir.$file_name; if(ssh2_scp_recv($connection, $remote_file,$local_file)){ echo "File ".$file_name." was copied to $local_dir<br />"; } } } fclose($stream); ?> 

I think my $ remote = "$ remote_dir"; incorrectly, and frankly, I have $ filename, when I want the whole directory, that’s all I have so far.

+7
source share
2 answers

Here is a little code on how to read a folder and upload all its files:

 <?php $host = 'localhost'; $port = 22; $username = 'username'; $password = 'password'; $remoteDir = '/must/be/the/complete/folder/path'; $localDir = '/can/be/the/relative/or/absolute/local/path'; if (!function_exists("ssh2_connect")) die('Function ssh2_connect not found, you cannot use ssh2 here'); if (!$connection = ssh2_connect($host, $port)) die('Unable to connect'); if (!ssh2_auth_password($connection, $username, $password)) die('Unable to authenticate.'); if (!$stream = ssh2_sftp($connection)) die('Unable to create a stream.'); if (!$dir = opendir("ssh2.sftp://{$stream}{$remoteDir}")) die('Could not open the directory'); $files = array(); while (false !== ($file = readdir($dir))) { if ($file == "." || $file == "..") continue; $files[] = $file; } foreach ($files as $file) { echo "Copying file: $file\n"; if (!$remote = @fopen("ssh2.sftp://{$stream}/{$remoteDir}{$file}", 'r')) { echo "Unable to open remote file: $file\n"; continue; } if (!$local = @fopen($localDir . $file, 'w')) { echo "Unable to create local file: $file\n"; continue; } $read = 0; $filesize = filesize("ssh2.sftp://{$stream}/{$remoteDir}{$file}"); while ($read < $filesize && ($buffer = fread($remote, $filesize - $read))) { $read += strlen($buffer); if (fwrite($local, $buffer) === FALSE) { echo "Unable to write to local file: $file\n"; break; } } fclose($local); fclose($remote); } 

You can also renew this code until (it will not copy directories) :

 while (false !== ($file = readdir($dirHandle))) { if ($file == "." || $file == "..") continue; echo "Copying file: $file\n"; if(!ssh2_scp_recv($connection, $remoteDir . $file, $localDir . $file)) echo "Could not download: ", $remoteDir, $file, "\n"; } 

If you do not use the full path in the remote folder, it will not work:

 opendir("ssh2.sftp://{$stream}{$remoteDir}") 
+18
source

Update: I was kindly fixed that it does not use sftp, but uses ftps instead. Here's the Stackoverflow link discussing using PHP to work with SFTP .

PHP docs already covers most of what you need for this. Here is an example of retrieving a list of contents in a remote directory:

 <?php // set up basic connection $ftp_server = "example.com"; $conn_id = ftp_ssl_connect($ftp_server); // login with username and password $ftp_user_name = "myuser"; $ftp_user_pass = "mypass"; $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); ftp_pasv($conn_id, true); // check connection if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!"; echo "Attempted to connect to $ftp_server for user $ftp_user_name"; exit; } else { echo "Connected to $ftp_server, for user $ftp_user_name"; } $buff = ftp_rawlist($conn_id, '.'); var_dump($buff); ftp_close($conn_id); ?> 
0
source

Source: https://habr.com/ru/post/949606/


All Articles