-1

I've tried several different methods of connecting to an SFTP server and in all cases I get a 500 error either when I connect or when I try to upload a file.

I've tried connecting to two different servers, getting the same results each time. I am however able to connect to both servers using a GUI interface client, no problem.

Using SSH2_connect:

//<!--************************************************************************-->
//<!--*** Connect using ssh2
//<!--************************************************************************-->

$conn = ssh2_connect($ftp_host, 22);
ssh2_auth_password($conn, $ftp_username, $ftp_password);

//<!--************************************************************************-->
//<!--*** Send the file
//<!--************************************************************************-->

ssh2_scp_send($conn, $localfile, $remote_file);

The connect call causes a 500 error.

I also tried using PHPSecLib, as recommended by so many people on StackExchange, but I run into the same problem.

include("common/PHP SFTP/Net/SFTP.php");

//<!--************************************************************************-->
//<!--*** Connect using sftp
//<!--************************************************************************-->

$sftp = new Net_SFTP($ftp_host);

//<!--************************************************************************-->
//<!--*** Log into ftp server
//<!--************************************************************************-->

if ($sftp->login($ftp_username, $ftp_password)) {

  //<!--************************************************************************-->
  //<!--*** Send local file via ftp
  //<!--************************************************************************-->

  if(!$sftp->put($remote_file, $localfile)){
     errorLog(1,"DEBUG","eft transfer","Fail");
  }

}else{
  errorLog(1,"DEBUG","sftp connection","Fail");
}

//<!--************************************************************************-->
//<!--*** Close the connection
//<!--************************************************************************-->

ftp_close($ftp_connection);

In this case creating a new Net_SFTP object causes no problems so it's not a pathing issue to the included PHPSecLib file, but as soon as I try to login to the ftp server it falls over with a 500 error.

Using phpinfo() I've confirmed that I have OpenSSL enabled on the server. SFTP protocol is also enabled. SSH2 DLL is also installed and enabled.

I don't know what else to look for.

These are the errors in my log file

[25-Sep-2017 11:53:15 America/New_York] PHP Notice:  Undefined index: pagedate in C:\inetpub\wwwroot\TimeSavr\Refresh\common\initialize.php on line 111
[25-Sep-2017 09:53:15 America/Edmonton] PHP Notice:  Undefined variable: content in C:\inetpub\wwwroot\TimeSavr\Refresh\common\common.php on line 8
[25-Sep-2017 09:53:15 America/Edmonton] PHP Warning:  include_once(Math/BigInteger.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 891
[25-Sep-2017 09:53:15 America/Edmonton] PHP Warning:  include_once(): Failed opening 'Math/BigInteger.php' for inclusion (include_path='.;C:\php\pear') in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 891
[25-Sep-2017 09:53:15 America/Edmonton] PHP Warning:  include_once(Crypt/Random.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 895
[25-Sep-2017 09:53:15 America/Edmonton] PHP Warning:  include_once(): Failed opening 'Crypt/Random.php' for inclusion (include_path='.;C:\php\pear') in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 895
[25-Sep-2017 09:53:15 America/Edmonton] PHP Warning:  include_once(Crypt/Hash.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 899
[25-Sep-2017 09:53:15 America/Edmonton] PHP Warning:  include_once(): Failed opening 'Crypt/Hash.php' for inclusion (include_path='.;C:\php\pear') in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 899
[25-Sep-2017 09:53:15 America/Edmonton] PHP Warning:  include_once(Crypt/Base.php): failed to open stream: No such file or directory in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 904
[25-Sep-2017 09:53:15 America/Edmonton] PHP Warning:  include_once(): Failed opening 'Crypt/Base.php' for inclusion (include_path='.;C:\php\pear') in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 904
[25-Sep-2017 09:53:16 America/Edmonton] PHP Fatal error:  Call to undefined function phpseclib_resolve_include_path() in C:\inetpub\wwwroot\TimeSavr\Refresh\common\PHP SFTP\Net\SSH2.php on line 1243
Vincent
  • 1,741
  • 23
  • 35
  • 1
    does the 500 error come with an error message? – apokryfos Sep 24 '17 at 23:01
  • 2
    Also, `ftp_close($ftp_connection);` looks really out of place there. – apokryfos Sep 24 '17 at 23:03
  • 1
    A generic 500 Internal Server Error usually means the error message is written to the web server log. – Devon Bessemer Sep 24 '17 at 23:13
  • I've updated my question with the errors in my log file. – Vincent Sep 24 '17 at 23:27
  • So I see in my error logs that it appears to be trying to connect over port 21, but that should be 22. The default value using phpseclib is 22, but even if I pass in the portnumber to "net_sftp($host,22)" the error messages are still showing port 21. Could this be the reason I'm getting a 500 error? – Vincent Sep 25 '17 at 00:03
  • Whoever down voted my question, please tell me why. Is this place not for seeking help? Or is my question not intelligent enough for you? – Vincent Sep 25 '17 at 00:55
  • The log file is *not* from the code you have posted. Post the relevant part of the log file. – Martin Prikryl Sep 25 '17 at 06:16
  • I've updated my question to show the errors from the PHP log file. – Vincent Sep 25 '17 at 15:58

1 Answers1

1

Turns out the phpseclib code was unable to find the following files:

Math/BigInteger.php
Crypt/Random.php
Crypt/Hash.php
Crypt/Base.php

...because the script relies on the php include path which by default is c:\php\pear.

So I solved it by adding the following two lines to the top of my script.

$currentdirectory = getcwd();
set_include_path(get_include_path().";".$currentdirectory.'/common/PHP SFTP');
Vincent
  • 1,741
  • 23
  • 35