downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ssh2_sftp_lstat> <ssh2_scp_recv
Last updated: Fri, 13 Nov 2009

view this page in

ssh2_scp_send

(PECL ssh2 >= 0.9.0)

ssh2_scp_sendSCP 経由でファイルを送信する

説明

bool ssh2_scp_send ( resource $session , string $local_file , string $remote_file [, int $create_mode = 0644 ] )

ローカルファイルシステムからリモートサーバに SCP プロトコルを使用してコピーします。

パラメータ

session

ssh2_connect() のコールによって取得した SSH 接続リンク ID。

local_file

ローカルファイルへのパス。

remote_file

リモートファイルへのパス。

create_mode

ファイルは create_mode で指定されたモードで作成されます。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例1 SCP 経由でのファイルのアップロード

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

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

参考



add a note add a note User Contributed Notes
ssh2_scp_send
stefanov at uk dot ibm dot com
02-May-2008 09:07
In addition to my previous post, I figured out that sftp->fopen->file_get_contents->fwrite has much better performance than ssh2_scp_send.

I've used the following code to test:

<?php
$srcFile
= '/var/tmp/dir1/file_to_send.txt';
$dstFile = '/var/tmp/dir2/file_received.txt';

// Create connection the the remote host
$conn = ssh2_connect('my.server.com', 22);

// Create SFTP session
$sftp = ssh2_sftp($conn);

$sftpStream = @fopen('ssh2.sftp://'.$sftp.$dstFile, 'w');

try {

    if (!
$sftpStream) {
        throw new
Exception("Could not open remote file: $dstFile");
    }
   
   
$data_to_send = @file_get_contents($srcFile);
   
    if (
$data_to_send === false) {
        throw new
Exception("Could not open local file: $srcFile.");
    }
   
    if (@
fwrite($sftpStream, $data_to_send) === false) {
        throw new
Exception("Could not send data from file: $srcFile.");
    }
   
   
fclose($sftpStream);
                   
} catch (
Exception $e) {
   
error_log('Exception: ' . $e->getMessage());
   
fclose($sftpStream);
}
?>

For the test I've sent three files with total size of 6kB, and the times to send including connect to the server were:

SFTP -> 15 sec.
ssh2_scp_send -> 22 sec.

Cheers,

Pimmy
stefanov at uk dot ibm dot com
25-Apr-2008 09:23
After some testing I figured out that ssh2_scp_send does not work exactly as the standard scp command:

- Works: ssh2_scp_send($conn, '/var/tmp/file_01.txt', /var/tmp/file_02.txt');
- Wrong: ssh2_scp_send($conn, '/var/tmp/file_01.txt', /var/tmp'); (Creates file with name 'tmp')
- Fails: ssh2_scp_send($conn, '/var/tmp/file_01.txt', /var/tmp/');
- Fails: ssh2_scp_send($conn, '/dirname', /var/tmp/'); (No recursion)
- Fails: ssh2_scp_send($conn, '/dirname/*', /var/tmp/'); (Cannot copy more than one file.)

Cheers,

Pimmy

ssh2_sftp_lstat> <ssh2_scp_recv
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites