0

By using followign code i get the result "Resource id #5" if I echo $stream

I use PHP 7.0 on apache (linux) phpinfo:

  • ssh2
  • extension version 0.12+dev
  • libssh2 version 1.5.0
  • banner SSH-2.0-libssh2_1.5.0*

    <?php
    // $server = ... etc.
    $connection = ssh2_connect($server, $port);   
    ssh2_auth_password($connection, $username, $password); 
    $sftp = ssh2_sftp($connection);
    
    $filename = 'test.csv';
    
    $stream = fopen("ssh2.sftp://" . intval($sftp) . "/dir/$filename", 'r'); 
    echo $stream; // Result: "Resource id #5"
    ?>
    

The result is "Resource id #5" and I don't know what I do wrong

Korty
  • 309
  • 2
  • 8
  • 19

1 Answers1

2

If you're trying to read the file you will have to do a fread() on the $stream. You can't just echo the result of a fopen.

Lulceltech
  • 1,662
  • 11
  • 22
  • $contents = fread($stream, filesize($remoteFile)); fclose($stream); Result: filesize(): stat failed for /dir/test.csv – Korty Apr 03 '19 at 14:27
  • There's actually a remote version of fread, I wanted you to go look at the PHP manual: https://www.php.net/manual/en/function.fread.php. The function you're looking for is: $contents = stream_get_contents($handle); fclose($handle); – Lulceltech Apr 03 '19 at 14:32
  • fgets() helped me, fread no way... – Korty Apr 03 '19 at 14:36