note 97997 modified in function.ssh2-scp-recv by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
This function can be used to copy all files into a directory on a remote server, using SSH and SCP.

<?php
$connection = ssh2_connect('host', 22);
ssh2_auth_password($connection, 'user', 'password');

$remote_dir="/remote_dir/";
$local_dir="/local_dir/";

$com ="ls $remote_dir";
$stream = ssh2_exec($connection, $com);
stream_set_blocking($stream,true);
$cmd=fread($stream,4096);

$arr=explode("\n",$cmd);

$total_files=sizeof($arr);

for($i=0;$i<$total_files;$i++){
	$file_name=trim($arr[$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);
?>

--was--
This function can be used to copy all files into a directory on a remote server, using SSH and SCP.

$connection = ssh2_connect('host', 22);
ssh2_auth_password($connection, 'user', 'password');

$remote_dir="/remote_dir/";
$local_dir="/local_dir/";

$com ="ls $remote_dir";
$stream = ssh2_exec($connection, $com);
stream_set_blocking($stream,true);
$cmd=fread($stream,4096);

$arr=explode("\n",$cmd);

$total_files=sizeof($arr);

for($i=0;$i<$total_files;$i++){
	$file_name=trim($arr[$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);

http://php.net/manual/en/function.ssh2-scp-recv.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.