尝试从 SFTP 位置下载文件。
我intval
在流上使用它,因为它旨在修复我在这里读到的错误:PHP 7 SSH2.SFTP stat() bug work around。我正在运行 PHP 7.0.33。
但是,它仍然在filesize
命令上失败
$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');
}
错误是:
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
我不明白为什么opendir
并且fopen
工作但filesize
失败了。任何指导将不胜感激。
谢谢
更新:
我放弃了试图让它发挥作用。我正在使用它。不太复杂,但它可以完成工作
copy("ssh2.sftp://".intval($sftp)."/{$remoteDir}{$file}", "localdata/$file");