使用PHP设置SSH连接和SFTP

I'm trying to set up a ssh connection, and later a sftp connection to transfer file via FTP to my server. I have used FileZilla to upload files successfully before.

These are my login terminal command and FileZilla login information:

ssh b0xxxxx@linux7.aa.bb.cc.dd
Host: sftp://linux7.aa.bb.cc.dd
Username: b0xx
Port: 22

Here is my php code:

<?php
    $ftp_server = "b0xx@linux7.aa.bb.cc.dd";
    $ftp_port = 22; 
    $ftp_user = "b0xx";
    $ftp_pass = "##";
    $connection = ssh2_connect($ftp_server, $ftp_port);
    echo "Successful!
";
    if (ssh2_auth_password($connection, $ftp_user, $ftp_pass)) {
      echo "Successful
";
    } else {
      die ('Failed...');
    }
    $sftp = ssh2_sftp($connection);
    ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);

?>

Question 1: Can I just use ftp functions to upload/download file from my server? Or do I must set up ssh?

Question 2: When I execute the code, the website only displays

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

which means that the code does not execute connection successfully. Does anyone know the cause of the bug?

Very much appreciate the help.

douding_1073
douding_1073 你检查了php错误日志文件中的相关消息吗?
4 年多之前 回复

2个回答



似乎未安装SSH2 PECL扩展。 安装扩展,然后尝试您的代码。</ p>
</ div>

展开原文

原文

Seems SSH2 PECL extension is not installed. Install the extension and then try your code.



  $ ftp_server =“b0xx@linux7.aa.bb.cc.dd”; 
</ code> </ pre>

b0xx@linux7.aa.bb.cc.dd”不是主机名。 主机名中不能包含“@”。 这可能是“username @ hostname”字符串,但 ssh_connect()</ code>不接受此表单中的字符串。</ p>

删除用户名部分 :</ p>

  $ ftp_server =“linux7.aa.bb.cc.dd”; 
</ code> </ pre>
</ div>

展开原文

原文

$ftp_server = "b0xx@linux7.aa.bb.cc.dd";

"b0xx@linux7.aa.bb.cc.dd" isn't a hostname. Hostnames can't have "@" in them. This is probably intended to be a "username@hostname" string, but ssh_connect() doesn't accept a string in this form.

Remove the username portion:

$ftp_server = "linux7.aa.bb.cc.dd";

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问