0

我正在尝试通过 SFTP 写东西,但发生了一些奇怪的事情。

这是一个通用示例脚本:

<?php

$resConnection = ssh2_connect("<url>");

if (ssh2_auth_password($resConnection, "<name>", '<password>')){
    //Initialize SFTP subsystem
    echo "connected";

    $resSFTP = ssh2_sftp($resConnection);
    $resFile = fopen("ssh2.sftp://{$resSFTP}/"."test", 'w');
    fwrite($resFile, "Testing");
    fclose($resFile);
} else {
    echo "Unable to authenticate on server";
}

它的输出是这样的:

root@0fb6a2a1af08:/var/www# php test.php
connected
Warning: fopen(ssh2.sftp://Resource id #5/test): failed to open stream: operation failed in /var/www/test.php on line 8

Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/test.php on line 9

Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/test.php on line 10

所以基本上它连接但不能写任何东西。具有相同凭据的相同代码在另一台机器上运行良好。

SFTP端的某些特定配置是否有可能阻止我写作?

4

1 回答 1

0

好的,它与此错误有关:

现在的解决方案intval是在文档中使用资源字符串:https ://www.php.net/manual/ru/function.ssh2-sftp.php

$stream = fopen('ssh2.sftp://' . intval($sftp) . '/path/to/file', 'r'); 
于 2020-02-19T12:37:52.700 回答