doujiao6507 2017-09-24 22:54
浏览 110
已采纳

尝试在PHP中使用SFTP导致500错误

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
  • 写回答

1条回答 默认 最新

  • dqsxsmi3704 2017-09-25 16:35
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站