phpsftplibssh2ssh2

Can connect but cannot write via SFTP


I'm trying to write stuff via SFTP and something strange is happening.

Here's a generic sample script:

<?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";
}

And the output for it is something like this:

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

So basically it connects but cannot write anything. The same code with the same credentials works fine on another machine.

Is it possible that some specific configuration on SFTP side prevents me from writing?


Solution

  • Ok, it is connected with this bugs:

    The solutions for now is to use intval around the resource string as it is said in docs: https://www.php.net/manual/ru/function.ssh2-sftp.php

    $stream = fopen('ssh2.sftp://' . intval($sftp) . '/path/to/file', 'r');