PHP试图通过SFTP下载文件,但在计算文件大小时收到stat失败错误



尝试从SFTP位置下载文件。

我在流上使用intval,因为这是为了修复我在这里读到的一个错误:PHP 7 SSH2.SFTP stat((错误。我运行的是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

我不明白为什么opendirfopen工作,但filesize失败了。如有任何指导,我们将不胜感激。

谢谢

更新:

我放弃了努力让它发挥作用。我用这个来代替。不那么复杂,但它能完成的工作

copy("ssh2.sftp://".intval($sftp)."/{$remoteDir}{$file}", "localdata/$file");

我在php 8.0中遇到了同样的问题

尝试将filesize命令放在fopen之前。

最新更新