Dear libssh2 group,

This is the 2nd time I'm posting this. The last attempt didn't get through and isn't in the Archive.

Please, I need the simplest possible PHP coding example of using libssh or libssh2 to upload a single file using the sftp protocol in the following environment: Windows, PHP running via a local server URL in a browser (for example, the URL might be "localhost/upload.php"). My actual application is considerably more complex, but this simple example is all I need to get started.

I have downloaded zip file php_ssh2-1.2-7.4-ts-vc15-x64, and it contains php_ssh2.dll, which I assume goes into the PHP/ext directory/folder. I'm not aware of any other installation required.

I've been trying to understand this library for several weeks (part-time), including trying to read https://www.php.net/ssh2, and just been frustrated. I'm a beginner when it comes to cryptography.

So I am looking for SIMPLE instructions and a single example of simple PHP code to upload a file using sftp.

It should look something like the following working insecure example, but include a local encrypted (preferred) or unencrypted private key pathnanme. It may optionally include the certificate pathname, if that is required. It should not prompt the user for any information, just upload the file silently like the following working code does insecurely:

<?php
$c=GetConfigArr("secret.json");

echo "Uploading...".<br>;

$ftp=ftp_connect($c['host'],$c['port'],$c['timeoutSec']);
if ($ftp===false)
    exit("Cannot connect to remote server (host)");

$R=ftp_login($ftp,$c['user'],$c['password']);
if (!$R)
    {
    ftp_close($ftp);
    exit("Cannot login to remote server (host)");
    }

$file="test.txt";
$remoteDir="public_html/new/";
$R=ftp_put($ftp,$remoteDir.$file,$file,FTP_ASCII);
if (!$R)
    {
    ftp_close($ftp);
    exit("Cannot copy file '$file' to '$remoteDir$file' on remote server");
    }

ftp_close($ftp);

echo "...successfully done".<br>;

function GetConfigArr($file)
    {
    global $pagesDir;
    $configStr=@file_get_contents($file);
    if ($configStr===false)
        exit("*** Config file '$file' is missing from $pagesDir)";
    return json_decode($configStr,true);
    } // GetConfigArr
?>

David Spector
PHP developer
Springtime Software
Maine, USA
_______________________________________________
libssh2-devel https://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel

Reply via email to