本文介绍了使用 PHP 通过 SFTP 传输文件以供下载非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够通过 SFTP 从服务器获取文件,以便我的 Web 服务器允许用户随后下载它.为了实现这一点,我使用了 phpseclib 库.不幸的是,这真的很慢,因为我只能达到 300-350kb/s 的速度,而在从托管原始文件的服务器直接下载之前,我可以达到超过 1mb/s.由于我们公司的一些内部原因,我们不得不将这些文件重新定位到另一台只打开了 22 端口的服务器上,因此当用户访问以下链接时:http://my.website.com/some/path/getfile/getfile.php?filename=someFileName.extension 我的 php 脚本通过 SSH 连接到托管文件的服务器,然后将指定的文件下载到 php://output 流供用户下载.

I want to be able to get a file through SFTP from a server in order for my web server to allow users to download it afterwards. To achieve this, I am using the phpseclib library. Unfortunately, this is really slow as I can only achieve speeds of 300-350kb/s when I could achieve over 1mb/s before when directly downloading from the server hosting the original files. For some internal reasons in our company, we have to relocate these files on an other server which only has the port 22 opened, so when a user goes on the following link: http://my.website.com/some/path/getfile/getfile.php?filename=someFileName.extension my php script connects via SSH to the server that hosts the files, then downloads the specified file to the php://output stream for the user to download it.

这是我的 php 脚本:

Here is my php script:

error_reporting(E_ERROR);
include('Net/SFTP.php');
include('Crypt/RSA.php');

if (!isset($_GET['filename']))
    die();

# This if statement is there as a layer of protection to prevent users from going elsewhere
# in the filesystem of the server.
if (!strpos($_GET['filename'], '..') and !strpos($_GET['filename'], '/') and !strpos($_GET['filename'], '\\')) {
    $sftp = new Net_SFTP('my.website.com');
    $key = new Crypt_RSA();
    $key->loadkey(file_get_contents('key.pvt'));
    if (!$sftp->login('loginUserName', $key)) {
        exit('Login Failed');
    }

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$_GET['filename']);
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate');
    header('Accept-Ranges: bytes');
    header('Expires: 0');
    header('Pragma: public');
    header('Content-Length: ' . $sftp->size('transfer/'.$_GET['filename']));
    ob_clean();
    flush();

    set_time_limit(3600);
    $sftp->get('transfer/'.$_GET['filename'], 'php://output');
}

这行得通,虽然如前所述它真的很慢.什么可能导致这种放缓?

This works, though as mentioned it is really slow. What could be causing this slow down?

推荐答案

phpseclib 使用 mcrypt(如果可用),否则使用它自己的纯 PHP 实现.如果你执行 phpinfo() 你看到 mcrypt 安装了吗?

phpseclib uses mcrypt if it's available and it's own pure PHP implementation otherwise. If you do phpinfo() do you see mcrypt installed?

此外,SSH2 支持多种加密算法,其中一些算法比其他算法更快.可能是您与 phpseclib 进行比较的客户端只是使用了比 phpseclib 实现的算法更快的算法.

Also, SSH2 supports a multitude of encryption algorithms and some are faster than others. It could be that the client you're comparing phpseclib against is simply using a faster algorithm than phpseclib has implemented.

最后,您使用的是什么版本的 phpseclib?最新的是0.3.7.

Finally, what version of phpseclib are you using? The latest is 0.3.7.

这篇关于使用 PHP 通过 SFTP 传输文件以供下载非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 08:02