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");