php-7 - PHP 7 SSH2.SFTP stat() 错误解决方法

标签 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 )

最佳答案

报价PHP ssh2.sftp opendir/readdir fix ,

Instead of using "ssh2.sftp://$sftp" as a stream path, convert $sftp to an integer like so: "ssh2.sftp://" . intval($sftp) . "/". Then it will work just fine.



变更原因如下:

PHP 5.6.28 (and apparently 7.0.13) introduced a security fix to URL parsing, that caused the string interpolation of the $sftp resource handle to no-longer be recognized as a valid URL. In turn, that causes opendir(), readdir(), etc. to fail when you use an $sftp resource in the path string, after an upgrade to one of those PHP versions.



至于其他图书馆……我知道的其他图书馆只有 phpseclib ,它有一个用于 libssh2 的模拟器:

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

那个“模拟器”当然可以改进。像应该添加 composer.json 文件等。

关于php-7 - PHP 7 SSH2.SFTP stat() 错误解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40921520/

相关文章:

PHP:命名空间中的返回值声明

php - dechex 函数输出错误的结果

php - 替换 @mysql_fetch_array 进行 php7 升级

php - 函数 uasort 在 PHP 5.5 和 PHP 7.0 中的不同行为

node.js - 连接到 sftp.webtrends.com 时没有匹配的主机 key

node.js - 查询远程服务器的操作系统

ssh - 在 Tectia 和 WinSCP SFTP 客户端中验证 SSH 主机 key

ssh - sftp 失败,错误为 'message too long'

php - 通过使用元素分组并通过另一个过滤来复制 CSV 文件

node.js - Angular 6 SSH2 - 如何使用 promise 处理错误?