2

Trying to download files from a SFTP location.

I'm using intval on the stream as this is designed to fix a bug I've read about here: PHP 7 SSH2.SFTP stat() bug work around. I'm running PHP 7.0.33.

However, it still fails on the filesize command

$connection = ssh2_connect('remote.host', 22);

ssh2_auth_password($connection, 'user', 'pass');

$sftp = ssh2_sftp($connection);

$remoteDir = '/Data/';


$handle = opendir("ssh2.sftp://".intval($sftp)."$remoteDir"); //works with intval, fails without  

while (false !== ($entry = readdir($handle))){

        $local = fopen('localdata/'.$entry, 'w');

        $remote = fopen("ssh2.sftp://".intval($sftp)."$remoteDir$entry", 'r'); //works with intval, fails without

        $fileSize = filesize("ssh2.sftp://".intval($sftp)."$remoteDir$entry"); //fails with or without intval

        $buffer = fread($remote, $filesize - $read);

        fwrite($local, '$buffer');

}

The error is:

PHP Warning:  filesize(): stat failed for ssh2.sftp://5/ Data/remote_file.csv in /var/www/html/sftp_test.php on line 58
Segmentation fault

I don't understand why opendir and fopen work but filesize fails. Any guidance would be appreciated.

Thank you

UPDATE:

I gave up trying to get this to work. I'm using this instead. Less sophisticated but it does the job

copy("ssh2.sftp://".intval($sftp)."/{$remoteDir}{$file}", "localdata/$file");
user2029890
  • 2,493
  • 6
  • 34
  • 65
  • 1
    PHP should never segfault. I would check for similar bugs on their bug tracker and submit a new one if you don't find an existing one. – Sean Bright Feb 25 '20 at 19:19

1 Answers1

0

I had the same issue with php 8.0.

Try putting the filesize command before the fopen.