首页 > 解决方案 > PHP 尝试通过 SFTP 下载文件但在计算文件大小时收到 stat failed 错误

问题描述

尝试从 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");

标签: phpsftpssh2-sftp

解决方案


推荐阅读