PHP 7 SSH2.SFTP stat()bug解决了

时间:2016-12-01 22:25:23

标签: php-7 ssh2-sftp

我有一个使用SFTP连接下载文件的应用程序。它在PHP 5.6中正常工作,而不是在PHP 7中工作。我得到的错误如下:

PHP警告:filesize():ssh2.sftp的统计信息失败...

我的代码如下:

 public function retrieveFiles($downloadTargetFolder,$remoteFolder = '.') {

            $fileCount = 0;

                echo "\nSftpFetcher retrieveFiles\n";

        $con = ssh2_connect($this->host,$this->port) or die("Couldn't connect\n");
        if($this->pubKeyFile){
                $isAuth = ssh2_auth_pubkey_file($con, $this->user, $this->pubKeyFile, $this->privKeyFile);
        } else {
                $isAuth = ssh2_auth_password($con,  $this->user,  $this->pass);
        };


        if ($isAuth) {

                $sftp = ssh2_sftp($con);
                $rd = "ssh2.sftp://{$sftp}{$remoteFolder}";

                if (!$dir = opendir($rd)) {
                        echo "\nCould not open the remote directory\n";
                } else {
                        $files = array();
                                while (false != ($file = readdir($dir))) {
                                    if ($file == "." || $file == "..")
                                        continue;
                                    $files[] = $file;
                                }

                        if (is_array($files)) {
                            foreach ($files as $remoteFile) {
                                                echo "\ncheck file: $remoteFile vs filter: " . $this->filter."\n";
                                if ($this->filter !== null && strpos($remoteFile,$this->filter) === false) {
                                    continue;
                                }
                                                echo "file matched\n";
                                $localFile = $downloadTargetFolder . DIRECTORY_SEPARATOR . basename($remoteFile);


                                //$result = ftp_get($con,$localFile,$remoteFile,FTP_BINARY);
                                $result = true;
                                // Remote stream
                                if (!$remoteStream = @fopen($rd."/".$remoteFile, 'r')) {
                                        echo "Unable to open the remote file $remoteFolder/$remoteFile\n";
                                        $return = false;
                                } else {
                                        // Local stream
                                        if (!$localStream = @fopen($localFile, 'w')) {
                                                echo "Unable to open the local file $localFile\n";
                                                $return = false;
                                        } else {
                                                // Write from our remote stream to our local stream

                                                $read = 0;
                                                $fileSize = filesize($rd."/".$remoteFile);
                                                while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) {
                                                        $read += strlen($buffer);
                                                        if (fwrite($localStream, $buffer) === FALSE) {
                                                                echo "Unable to write the local file $localFile\n";
                                                                $return = false;
                                                                break;
                                                        }
                                                }


                                                echo "File retrieved";
                                                // Close
                                                fclose($localStream);
                                                fclose($remoteStream);

                                        }

                                }

                                if ($result) {
                                    $fileCount++;
                                }
                            }
                        }

                        ssh2_exec($con, 'exit');
                        unset($con);
                }

        } else {
                echo "Error authenticating the user ".$this->user."\n";
        }

            return $fileCount;

    }
}

经过一些研究后,我发现stat()存在问题:

http://dougal.gunters.org/blog/2016/01/18/wordpress-php7-and-updates-via-php-ssh2/ https://bugs.php.net/bug.php?id=71376

我的问题

根据我目前的代码,是否有允许我通过SFTP下载的解决方法,或者是否有其他人可以推荐使用的库?

我的PHP版本: PHP 7.0.8-0ubuntu0.16.04.3(cli)(NTS)

1 个答案:

答案 0 :(得分:7)

引用PHP ssh2.sftp opendir/readdir fix

  

不要将"ssh2.sftp://$sftp"用作流路径,而是将$ sftp转换为如此的整数:"ssh2.sftp://" . intval($sftp) . "/"。然后就可以了。

更改的原因如下:

  

PHP 5.6.28(显然是7.0.13)引入了URL解析的安全修复程序,导致$ sftp资源句柄的字符串插值不再被识别为有效的URL。反过来,在升级到其中一个PHP版本之后,当你在路径字符串中使用$ sftp资源时,会导致opendir(),readdir()等失败。

至于其他图书馆......只有我知道的其他图书馆是phpseclib,它有一个针对libssh2的各种模拟器:

https://github.com/phpseclib/libssh2-compatibility-layer

那个&#34;模拟器&#34;当然可以改进。就像composer.json文件应该添加等等。