PHP 8.6.0 Beta 1 is available for testing

ssh2_scp_send

(PECL ssh2 >= 0.9.0)

ssh2_scp_sendEnvía un fichero mediante SCP

Descripción

function ssh2_scp_send(
resource $session,
string $local_file,
string $remote_file,
int $create_mode = 0644
): bool

Copia un fichero desde el sistema de ficheros local a un servidor remoto usando el protocolo SCP.

Parámetros

session
Un identificador de enlace de conexión a SSH, obtenido desde una llamada a ssh2_connect() .
local_file
Ruta del fichero local.
remote_file
Ruta del fichero remoto.
create_mode
El fichero debe ser creado con el modo especificado por create_mode.

Valores devueltos

Esta función retorna true en caso de éxito o false si ocurre un error.

Ejemplos

Ejemplo #1 Cargar un fichero via SCP

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>

Véase también

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes 4 notes

up
19
stefanov at uk dot ibm dot com
18 years ago
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
up
1
stefanov at uk dot ibm dot com
18 years ago
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
up
0
emmanuel dot kartmann at prosdk dot com
16 years ago
On Windows, I had trouble using ssh2_scp_send: files copied to a remote server where incomplete (truncated) and/or locked (error message : "access denied"). The back-end is also on Windows, using CopSSH (cygwin-based SSH server).
The SSH session was kept open - and the file were never flushed to disk.
There's a workaround though - make an explicit call to "exit" to close the session (flushing file content to disk):
<?php
 $objConnection = ssh2_connect($strHost, $strPort, $methods, $callbacks);
 ssh2_auth_password($objConnection, $strUser, $strPassword);
 ssh2_scp_send($objConnection , $strSource, $strDest);
 // Add this to flush buffers/close session
 ssh2_exec($objConnection, 'exit');
?>
up
-4
j dot patus at simpledino dot com
9 years ago
In case the command:
$sftpStream = @fopen('ssh2.sftp://'.$sftp.$dstFile, 'w');
causes you a segmentation fault, try using:
$sftpStream = @fopen('ssh2.sftp://'.intval($sftp).$dstFile, 'w');
Credit: http://stackoverflow.com/questions/7414175/php-sftp-seg-fault 
+add a note

AltStyle によって変換されたページ (->オリジナル) /