ssh2_scp_recv

(PECL ssh2:0.10-0.9)

ssh2_scp_recv — Request a file via SCP

Описание

bool ssh2_scp_recv ( resource $session , string $remote_file , string $local_file )

Copy a file from the remote server to the local filesystem using the SCP protocol.

Список параметров

session

An SSH connection link identifier, obtained from a call to ssh2_connect().

remote_file

Path to the remote file.

local_file

Path to the local file.

Возвращаемые значения

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.

Примеры

Пример #1 Downloading a file via SCP

<?php
$connection 
ssh2_connect('shell.example.com'22);
ssh2_auth_password($connection'username''password');

ssh2_scp_recv($connection'/remote/filename''/local/filename');
?>

Смотрите также

Коментарии

If you scan a remote directory for a list of files by using opendir and readdir on a path obtained from ssh2_sftp(), you'll end up with a list of full file paths containing the ssh2.sftp://$sftp prefix, where $sftp is the sftp resource string returned by ssh2_sftp. 

If you later attempt to use ssh2_scp_recv() (even on the same connection!) to copy files locally from the remote path, the copy operation will fail because ssh2_scp_recv() doesn't like the wrapper path prefix. It expects remote file references to be rooted at the base of the remote file system (eg., '/').

Moral: ssh2_scp_recv() likes paths rooted at the file system root on the remote machine, not full network paths. This routine will fail with an obscure message about not being able to copy the files - yet when you go look it works fine. Worse, when you connect and copy them from an scp shell session, it also works fine.
2006-08-08 16:51:21
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
If when trying to use this function you get "Warning: ssh2_scp_send(): Failure creating remote file," then try using

$data = file_get_contents("ssh2.sftp://$sftp/path/to/file");
2008-03-26 14:23:18
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
Problem:
  can't pass an array to sftp_scp_rec()
  also: for Windows: sftp was not happy when connecting to   a  Windows dir. 
  eg: sftp_scp_rec($connection, "remote/directory","local/$directory[$i]");
Solution:
The array was placing whitespace on the end of the variable.
trim(" ","",$directory[$i]);

connecting to Windows dir:
sftp_scp_rec($connection, "linux/directory","\\windows\share[$i]");

This script backs up remote Linux directories to a local Windows directory based on modified dates of files.

The Idea is to get a complete back up of a directory via ftp client etc. 
Then when the script runs it will back up newly modified files only.

<?php
//error_reporting(0);

$file        ='';
$arr2        ='';
$arr1        ='';
$ldir         '';

$remotedirs[0]     = "Domain name or IP address/";
$remotedirs[1]     = "Domain name or IP address/";
$remotedirs[2]     = "Domain name or IP address/";
$remotedirs[3]     = "Domain name or IP address/";

   
$fh fopen("c:\dirlist.txt","w");
   
fwrite($fh" ");
   
fclose($fh);
foreach (
$remotedirs as $val){
echo 
$remotedir "/data/www/$val/";
$localdir     "\\\\192.168.0.234\\C$\\xampp\\htdocs\\$val\\";
backupwebsites($remotedir,$localdir);
}
function 
backupwebsites($remotedir,$localdir){
   
$connection ssh2_connect(Host IP or Domain22);
   
$com ="ls -R -lt $remotedir";
   
ssh2_auth_password($connection'user''password');
   
$stream ssh2_exec($connection$com );
   
stream_set_blocking($streamtrue);
   
$output stream_get_contents($stream);

   
$fh fopen("c:\dirlist.txt","a+");
   
fwrite($fh$output);
   
fclose($fh);
   
$handle = @fopen('c:\dirlist.txt'"r");
if (
$handle) {
   while (!
feof($handle)) {
       
$lines[] = fgets($handle4096);
   }
   
fclose($handle);
}
    foreach (
$lines as $val)
    {
       
$yr date('Y-m-d');
       
$i++;
       
$arr1=split("200",$val);
       
$arr2=explode(" ",$arr1[1]);
        if(
"200".$arr2[0]==$yr)
        {
           
//if("200".$arr2[0]=='2008-04-21'){    //for testing
           
$remotedir $remotedir.$arr2[2];
           
$cpy=$arr2[2];
           
$file $localdir;
           
glue($connection,$remotedir,$localdir,$cpy);
        }
    }
}   
//echo $i;
function glue($connection,$remotedir,$localdir,$cpy){
   
$ldir[0] = "$localdir";
   
$ldir[1]="$cpy";
   
$file $ldir[0].$ldir[1];
   
$file trim($file);
     
$file;
 
gop($connection,$remotedir,$file);
}
function 
gop($connection,$remotedir,$file){
echo 
$file;

ssh2_scp_recv(
$connection
$remotedir,
$file);
}

?>
2008-04-23 14:10:04
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
Being still in Beta, I guess it will have it quirks. One I found was that if the remote filename has brackets in it, it just wont work.

I should file it as a bug report, but find the process all intimidating really - I am always just a stupid user who does not know what he is on about!

But anyway - the work around of renaming the file before downloading it works fine.

use:
$_new_path = str_replace("(", "", $_path );
$_new_path = str_replace(")", "", $_new_path );

ssh2_sftp_rename( $sftp,  $_path, $_new_path );

To tell you the truth I haven't determined if it is the open or close bracket, or both. Someone else can investigate that.

Meanwhile, it would be good to know all the bad filename characters which this function has problems with, no doubt there are more.
2008-05-28 06:19:17
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
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);
?>
2010-05-19 18:16:32
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
Автор:
Trying to get a remote file with spaces in its name?

Never forget to use quotes in the remote filename, but never in the local one.

Example:

<?php
$remote_file_name
="my name with spaces.ext"

ssh2_scp_recv($cn,$remote_file_name,$local_path."/".$remote_file_name); // ERROR

ssh2_scp_recv($cn,"\"".$remote_file_name."\"",$local_path."/".$remote_file_name); //OK

ssh2_scp_recv($cn,"\"".$remote_file_name."\"","\"".$local_path."/".$remote_file_name."\""); //ERROR
?>

So, the quotes in the file name are needed only for remote, not for local.
2011-05-25 04:37:46
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
Transferred files can be truncated if you do not call ssh2_exec($connection,'exit').  ssh2_scp_send will return true even if the file did not completely transfer.

eg.

ssh2_scp_send($connection, $infile, $remotefile, 0644);
ssh2_exec($connection, 'exit'); // exit the ssh2 connection
2012-10-03 15:47:32
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
Автор:
May fail when trying to get a remote file from the user's home directory when you try to use a path like '~/filename.txt' .  It appears to dislike tildes, so use the full file path.
2013-06-25 00:14:54
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
Автор:
If you are getting the error - ssh2_scp_recv(): Unable to receive remote file

Try using file_get_contents("ssh2.sftp://$sftp$fileLocation") and file_put_contents() as a workaround-- this is after I tried all sorts of different remote file path formats to no avail.
2013-07-30 20:40:15
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html
In case if you get:

Warning: ssh2_scp_recv(): Unable to receive remote file in /some-php-file.php on line 2

Use `stream_get_contents` function.

Example:
<?php
$stream 
= @fopen("ssh2.sftp://$sftp$remoteFile"'r');
if (! 
$stream) {
        throw new 
\Exception("Could not open file: $remoteFile");
}

$contents stream_get_contents($stream);
file_put_contents ($localFile$contents);
@
fclose($stream);
?>
2016-07-20 22:56:13
http://php5.kiev.ua/manual/ru/function.ssh2-scp-recv.html

    Поддержать сайт на родительском проекте КГБ