0

我正在使用PHPSecLib的 SFTP 功能从 FTP 服务器下载文件。

线

$sftp->get($fname);

如果文件最大为 200MB,则工作正常,但如果为 300MB,浏览器会响应“Firefox 无法在 [download.php] 找到文件”。也就是说,它说它找不到我用于下载远程文件的 php 文件。

一开始我以为是memory_limitphp.ini中的设置造成的,但是设置为128M还是350M都没有关系;200MB 文件仍然有效,300MB 文件失败。它在十秒钟后失败,所以max_execution_time或者max_input_time似乎不是罪魁祸首。有什么问题?

4

2 回答 2

1

首先,我强烈建议您将代码set_time_limit(0);放在 php 文件的最顶部(甚至在任何包含之前),因为您正在处理您不知道它们将花费多少时间的操作。

我想说这是网络服务器/浏览器在“长时间”内不发送/接收任何数据而超时的情况。为了解决这个问题,我们必须稍微修改一下 SFTP.php 文件,即 Net_SFTP 类,然后转到 get 方法(如果您有 phpseclib 0.3.1,则为第 1482 行)并在您唯一的“while”控制结构中添加一些内容有(我将在下面粘贴整个函数),添加以下代码:

if (strtolower(PHP_SAPI) != 'cli') { // run this if request is handled by a webserver (like your case)
    $my_iter++;

    if($my_iter > 1024){
        $my_iter = 0; // reset the counter
        echo "transferring ... " . date("G:i:s") . "<br />"; // send something to the buffer
    }

    // flush the buffers and prevent the timeout by actually outputting something to the browser
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
    usleep(100); // just in case, try removing this delay
}

它基本上不时输出一些东西(这段时间的 1024 次迭代)并刷新缓冲区以实际向浏览器输出一些东西。随意调整值。由于这些问题,这是不打算从网络服务器运行的代码(SFTP 类)。我的意思是,你可以,但你会遇到一些问题,比如这个。

此外,如果您尝试发送(),您将不得不对相应的方法进行类似的修改,但希望这将解决您的问题(至少这解决了我在本地开发盒上的超时问题)。

现在,按照承诺,完整的方法修改如下;-)

function get($remote_file, $local_file = false)
{
    if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
        return false;
    }

    $remote_file = $this->_realpath($remote_file);
    if ($remote_file === false) {
        return false;
    }

    $packet = pack('Na*N2', strlen($remote_file), $remote_file, NET_SFTP_OPEN_READ, 0);
    if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
        return false;
    }

    $response = $this->_get_sftp_packet();
    switch ($this->packet_type) {
        case NET_SFTP_HANDLE:
            $handle = substr($response, 4);
            break;
        case NET_SFTP_STATUS: // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
            $this->_logError($response);
            return false;
        default:
            user_error('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS', E_USER_NOTICE);
            return false;
    }

    if ($local_file !== false) {
        $fp = fopen($local_file, 'wb');
        if (!$fp) {
            return false;
        }
    } else {
        $content = '';
    }

    $read = 0;
    while (true) {
        if (strtolower(PHP_SAPI) != 'cli') { // run this if request is handled by a webserver (like your case)
            $my_iter++;

            if($my_iter > 1024){
                $my_iter = 0; // reset the counter
                echo "transferring ... " . date("G:i:s") . "<br />"; // send something to the buffer
            }

            // flush the buffers and prevent the timeout by actually outputting something to the browser
            ob_end_flush();
            ob_flush();
            flush();
            ob_start();
            usleep(100); // just in case, try removing this delay
        }

        $packet = pack('Na*N3', strlen($handle), $handle, 0, $read, 1 << 20);
        if (!$this->_send_sftp_packet(NET_SFTP_READ, $packet)) {
            if ($local_file !== false) {
                fclose($fp);
            }
            return false;
        }

        $response = $this->_get_sftp_packet();
        switch ($this->packet_type) {
            case NET_SFTP_DATA:
                $temp = substr($response, 4);
                $read+= strlen($temp);
                if ($local_file === false) {
                    $content.= $temp;
                } else {
                    fputs($fp, $temp);
                }
                break;
            case NET_SFTP_STATUS:
                $this->_logError($response);
                break 2;
            default:
                user_error('Expected SSH_FXP_DATA or SSH_FXP_STATUS', E_USER_NOTICE);
                if ($local_file !== false) {
                    fclose($fp);
                }
                return false;
        }
    }

    if ($local_file !== false) {
        fclose($fp);
    }

    if (!$this->_send_sftp_packet(NET_SFTP_CLOSE, pack('Na*', strlen($handle), $handle))) {
        return false;
    }

    $response = $this->_get_sftp_packet();
    if ($this->packet_type != NET_SFTP_STATUS) {
        user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
        return false;
    }

    $this->_logError($response);

    // check the status from the NET_SFTP_STATUS case in the above switch after the file has been closed
    if ($status != NET_SFTP_STATUS_OK) {
        return false;
    }

    if (isset($content)) {
        return $content;
    }

    return true;
}
于 2013-03-22T15:20:34.623 回答
1

你还可以做点别的...

<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
$sftp->login('username', 'password');

$start = 0;
while (true) {
    $response = $sftp->get('1mb', false, $start, 1024);
    $start+= 1024;
    if (empty($response)) {
        break;
    }
    echo $response;
}

IE。以多个块下载文件。

于 2013-03-24T23:15:00.603 回答