Re: Hoping for a working example of SFTP in PHP

[email protected] ("R. Eric Wheeler")
Newsgroups php.general
Message-ID <CAOBuhSuqfiWu-qN_N2GZC-HM13ympkwF_EmUcKdg-3qVuebr-w@mail.gmail.com>
Just a few questions.  What web server do you use?  And how did you install
it on windows.  I'll do the same install.  It seems to me that it is
something to do with the web server.

It could even be that the stream is disabled on apache or fpm to prevent
people from trying to connect to root@localhost, or administrator@localhost.
You said ssh2_scp_send does work?


On Sat, Feb 6, 2021, 5:54 AM David Spector <[email protected]>
wrote:

> Dear Eric,
>
> I have tried your latest gist, edited for my environment as shown below.
> The result was a timeout in the browser, with eventual error message
> "The connection to the server was reset while the page was loading."
>
> When I add "exit;" after the call to ssh2_connect, the result is instant
> execution, with no output.
>
> When I move the "exit;" to after the call to ssh2_auth_pubkey_file, the
> result is instant execution, with no output.
>
> When I move the "exit;" to after the call to ssh2_sftp, the result is
> instant execution, with no output.
>
> So the timeout happens either in the file_exists call or the copy call.
>
> I don't know how to do any other debugging.
>
> Please help further.
>
> ====
> <?php declare(strict_types=1);
> // Tested on
> // PHP 7.4.14 (cli) (built Jan  5 2021 151229) ( ZTS Visual C++ 2017 x64 )
> // Windows 10 Pro
> // pecl ssh2 v1.2
>
> // Remote server
> // Gentoo GNU/Linux Base System release 2.7
>
> $c=GetConfigArr("secret.json");
> extract($c);
> define("N","<br>");
>
> error_reporting(E_ALL);
> ini_set("display_errors", "1");
>
> file_put_contents(__DIR__."\\test.txt", "worked!");
>
> $host = $hostIP;
> $port = $securePort;
>
> $connection = ssh2_connect($host, (int)$port);
>
> 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 Cpathtoyourfiles
> $privateKey = __DIR__."\\A2-nsr.key"; // id_rsa
> $publicKey = __DIR__."\\A2-nsr.pub"; // id_rsa.pub
> $username = $user;
> $passphrase = $passphrase; //"passphrase"; // set this to passphrase if
> you have one.
>
> $errorHandler = static function(
>      int $errorNumber,
>      string $errorString,
>      string $errorFile = '',
>      int $errorLine = 0,
>      array $errorContext = []
> ) use($connection) {
>      ssh2_disconnect($connection);
>      var_dump($errorContext);
>      throw new ErrorException($errorString, $errorNumber, 1, $errorFile,
> $errorLine);
> };
>
> set_error_handler($errorHandler);
>
> if(false === ssh2_auth_pubkey_file($connection, $username, $publicKey,
> $privateKey, $passphrase)) {
>      throw new Exception("Authentication failed.");
> }
>
> $sftp = ssh2_sftp($connection);
> $remoteFile = "/home/$user/$remoteDir/test.txt";
>
> $streamUrl = sprintf("ssh2.sftp//%d%s", (int)$sftp, $remoteFile);
> $localFile = __DIR__ . "\\test.txt";
>
> if(!file_exists($streamUrl)) {
>      if(true === copy($localFile, $streamUrl)) {
>          print sprintf("Successfully copied local file '%s' to remote
> file '%s'", $localFile, $streamUrl) . PHP_EOL;
>      } else {
>          print sprintf("Failed to copy local file '%s' to remote file
> '%s'", $localFile, $streamUrl) . PHP_EOL;
>      }
> } else {
>      print sprintf("File '%s' exists.", $streamUrl) . PHP_EOL;
> }
>
> unset($sftp); // If you don't unset $sftp ssh2_disconnect will throw a
> segfault.
> ssh2_disconnect($connection);
>
> function GetConfigArr($file)
>         {
>         global $pagesDir;
>         $configStr=@file_get_contents($file);
>         if ($configStr===false)
>                 exit("*** HMAC: Config file '$file' is missing from
> $pagesDir; please
> read ".
>                         "file HMAC\Installation.txt for the steps to set
> up your HMAC website.");
>         return json_decode($configStr,true);
>         } // GetConfigArr
> ?>
> ====
>
> David
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.