Skip to content

Instantly share code, notes, and snippets.

@sikofitt
Last active February 7, 2021 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sikofitt/3c6818648874ce091006ff58f8f489c4 to your computer and use it in GitHub Desktop.
Save sikofitt/3c6818648874ce091006ff58f8f489c4 to your computer and use it in GitHub Desktop.
Working example of the pecl ssh2 extension copying files to remote.
<?php declare(strict_types=1);
// Tested on
// PHP 7.4.14 (cli) (built: Jan 5 2021 15:12:29) ( ZTS Visual C++ 2017 x64 )
// Windows 10 Pro
// pecl ssh2 v1.2
// Remote server
// Gentoo GNU/Linux Base System release 2.7
error_reporting(E_ALL);
ini_set('display_errors', '1');
file_put_contents(__DIR__.'/sftp.test', 'worked!');
$host = 'your.remote.ip.address';
$port = 22;
$connection = ssh2_connect($host, $port); // let libssh2 figure it out.
if(!is_resource($connection)) {
throw new Exception('Connection failed.');
}
// Without the __DIR__ php will just try to get 'id_rsa' and will fail.
// if your keys are elsewhere __DIR__ should be changed to C:\path\to\your\files
$privateKey = __DIR__.'\your_private_key_file'; // id_rsa
$publicKey = __DIR__.'\your_public_key_file.pub'; // id_rsa.pub
$username = 'your_username';
$passphrase = ''; // set this to passphrase if you have one.
if(false === ssh2_auth_pubkey_file($connection, $username, $publicKey, $privateKey, $passphrase)) {
ssh2_disconnect($connection);
throw new Exception('Authentication failed.');
}
$sftp = ssh2_sftp($connection);
$streamUrl = sprintf("ssh2.sftp://%d", (int)$sftp);
$path = sprintf('%s/path/to/the/directory', $streamUrl);
$file = sprintf('%s/sftp.test', $path);
$localFile = __DIR__ . '/sftp.test';
if(!file_exists($file)) {
if(true === copy($localFile, $file)) {
print 'Successfully copied ' . $localFile . PHP_EOL;
}
} else {
print 'File exists' . PHP_EOL;
}
// using ssh2_scp_send, if all you need to do is upload a file.
// There doesn't seem to be another way to check if the file exists
// using just the ssh2 resource without using the ssh2.sftp stream wrapper.
// If you want to check if the file exists use ssh2_sftp_stat() it will
// return false if the file does not exist.
$file2 = '/path/to/the/file/you/want/to/write/sftp.test2';
$res = ssh2_scp_send($connection, $localFile, $file2);
if($res) {
print 'Sent file ' . $file . PHP_EOL;
} else {
print 'There was an error copying file to remote.' . PHP_EOL;
}
// List contents of remote directory
$i = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
while($i->valid()) {
print $i->getPathname();
if($i->isDir()) {
print $i->getPath() . PHP_EOL;
} else {
print $i->getFilename() . PHP_EOL;
}
$i->next();
}
// shutdown
ssh2_disconnect($sft); // If you don't unset $sftp ssh2_disconnect will throw a segfault.
ssh2_disconnect($connection);
@sikofitt
Copy link
Author

sikofitt commented Feb 2, 2021

It doesn't need one.

php -l pecl_ssh2.php

Output: No syntax errors detected in pecl_ssh2.php

https://www.php.net/manual/en/language.basic-syntax.phptags.php

If a file contains only PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.

@David263
Copy link

David263 commented Feb 2, 2021 via email

@sikofitt
Copy link
Author

sikofitt commented Feb 2, 2021

No worries!
Before php 5.6 It was standard to use them. Since then they have suggested not using them unless you have mixed content.

I Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment