PHP - SSH2 SFTP 下载进展顺利 [英] PHP - SSH2 SFTP Downloads With Progress

查看:37
本文介绍了PHP - SSH2 SFTP 下载进展顺利的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这值得一个问题,因为 StackOverflow 似乎没有足够的主题.

I feel this deserves a question as StackOverflow doesn't seem to have enough on the subject.

我喜欢进度条.但是,这一次我想做一些不同的事情.我想用PHP做以下事情,这个看起来比上一个难很多:

I love progress bars. However, this time I want to do something a little different. I want to do the following with PHP, and this one looks a lot harder than the last:

  • 通过 SFTP(通过 SSH?)执行下载
  • 向用户显示下载进度的可见指示
  • 启用暂停和继续下载

认为 FileZilla,但在浏览器中,使用 PHP 构建.我不想破解 Apache 或添加任何 Apache 模组.

Think FileZilla, but in-browser, built with PHP. I don't want to hack Apache or add any Apache mods.

有很多问题这里这里这里此处 用于基本的 SFTP 下载.

There are plenty of questions here, here, here and here for the basic SFTP download.

这些文档PHP 的 SSH2 扩展(我在时刻 - 您通过 pecl 安装它)和替代的 PHPSecLib,我没有使用,但可能会调查之后.
我的界面允许轻松换入/换出 - 编码到界面而不是实现等...

These document PHP's SSH2 extension (which I am using at the moment - you install this via pecl) and the alternative PHPSecLib, which I'm not using, but may look into later.
My interface allows swapping in/out easily - coding to the interface rather than the implementation etc...

太好了,但他们只是执行实际下载,仅此而已.

That's great, but they just perform the actual download and that's it.

PHP 有一个非常有趣的回调函数 stream_notification_callback您可以在此处阅读更多相关信息.

PHP has a really interesting callback called stream_notification_callback, which you can read more about here.

这看起来很棒,并且是很有希望的一步,直到有人查看 PHP 的源代码并发现,不幸的是,SSH2/SFTP 不允许与此集成.
感谢 hek2mgl 致力于研究此.

This looks great, and was a promising step until someone looked into the source code of PHP and found that, unfortunately, SSH2 / SFTP doesn't allow integration with this.
Thanks to hek2mgl for putting in the effort to research this.

stream_notification_callback 的想法是在每次检索数据时传递当前下载大小的通知;因此,提供使用当前下载量和总文件大小计算百分比所需的数据.但这不适用于 SSH2 和 SFTP...

The idea with stream_notification_callback was to pass a notification of the current download size every time data is retrieved; therefore giving the data required to calculate a percentage using the currently downloaded amount and the total file size. But that doesn't go with SSH2 and SFTP...

在我看来,这将是最难实现的.将数据下载到临时文件是可能的......这是我设法挖掘的内容:http://ee.php.net/manual/en/function.fread.php#84115 - 但是将这种代码与进度条集成似乎很疯狂.

In my opinion, this would be the hardest to accomplish. Downloading data to a temporary file would be possible... Here's what I managed to dig up: http://ee.php.net/manual/en/function.fread.php#84115 - But integrating that sort of code with the progress bar seems crazy.

还有cURL,但是我没有看到通过SFTP 尽可能暂停/恢复下载.如果我错了,请纠正我.

There's also cURL, however I didn't see the pausing / resuming of downloads as possible with this over SFTP. Correct me if I'm wrong.

那么,我将如何使用 PHP 在浏览器中集成上述要求?忘记客户端的东西,只需将数据发送到浏览器就足够了,因此建议执行此操作会很棒.

So, how would I go about integration of the aforementioned requirements in-browser using PHP? Forget the client-side stuff, just getting the data to the browser is enough, so recommendations to perform this would be great.

推荐答案

这是我从 本网站:

    // Write from our remote stream to our local stream
    $read = 0;
    $fileSize = filesize("ssh2.sftp://$sftp/$fileName");
    while ($read < $fileSize && ($buffer = fread($remoteStream, $fileSize - $read))) {
        // Increase our bytes read
        $read += strlen($buffer);

        // Write to our local file
        if (fwrite($localStream, $buffer) === FALSE) {
            throw new Exception("Unable to write to local file: /localdir/$fileName");
        }
    }

由于您拥有文件的大小并且您使用 fread 从中读取,因此确定您的进度是微不足道的.

Since you have the size of the file and you read from it with fread it is trivial to determine your progress.

如果用户点击暂停按钮(在 iframe 中执行此操作,这样您就不会停止主脚本),则可以通过检查您设置的 APC 中的值来完成暂停.

Pausing can be done by checking a value from lets say APC that you set if user clicks on the Pause button (do this in an iframe so you do not stop the main script).

这篇关于PHP - SSH2 SFTP 下载进展顺利的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆