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

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)