流 FTP 下载到输出

2022-01-09 00:00:00 ftp php

我正在尝试通过 HTTP 从 FTP 将文件流式传输/管道传输到用户的浏览器.也就是说,我正在尝试在 FTP 服务器上打印文件的内容.

I am trying to stream/pipe a file to the user's browser through HTTP from FTP. That is, I am trying to print the contents of a file on an FTP server.

这是我目前所拥有的:

public function echo_contents() {                    
    $file = fopen('php://output', 'w+');             

    if(!$file) {                                     
        throw new Exception('Unable to open output');
    }                                                

    try {                                            
        $this->ftp->get($this->path, $file);         
    } catch(Exception $e) {                          
        fclose($file);  // wtb finally               

        throw $e;                                    
    }                                                

    fclose($file);                                   
}                                                    

$this->ftp->get 看起来像这样:

public function get($path, $stream) {
    ftp_fget($this->ftp, $stream, $path, FTP_BINARY);  // Line 200
}

使用这种方法,我只能将小文件发送到用户的浏览器.对于较大的文件,不会打印任何内容,并且出现致命错误(可从 Apache 日志中读取):

With this approach, I am only able to send small files to the user's browser. For larger files, nothing gets printed and I get a fatal error (readable from Apache logs):

PHP 致命错误:第 200 行/xxx/ftpconnection.php 中允许的内存大小为 16777216 字节已用尽(尝试分配 15994881 字节)

PHP Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 15994881 bytes) in /xxx/ftpconnection.php on line 200

我尝试用 php://stdout 替换 php://output 没有成功(似乎没有任何东西发送到浏览器).

I tried replacing php://output with php://stdout without success (nothing seems to be sent to the browser).

如何在将数据发送到浏览器的同时有效地从 FTP 下载?

How can I efficiently download from FTP while sending that data to the browser at the same time?

注意:我不想使用 file_get_contents('ftp://user:pass@host:port/path/to/file'); 或类似的.

Note: I would not like to use file_get_contents('ftp://user:pass@host:port/path/to/file'); or similar.

推荐答案

找到了解决方案!

创建一个套接字对(匿名管道?).使用非阻塞ftp_nb_fget函数写入管道的一端,echo管道的另一端.

Create a socket pair (anonymous pipe?). Use the non-blocking ftp_nb_fget function to write to one end of the pipe, and echo the other end of the pipe.

测试为快速(在 100Mbps 连接上轻松达到 10MB/s),因此没有太多 I/O 开销.

Tested to be fast (easily 10MB/s on a 100Mbps connection) so there's not much I/O overhead.

请务必清除所有输出缓冲区.框架通常会缓冲您的输出.

public function echo_contents() {
    /* FTP writes to [0].  Data passed through from [1]. */
    $sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);

    if($sockets === FALSE) {
        throw new Exception('Unable to create socket pair');
    }

    stream_set_write_buffer($sockets[0], 0);
    stream_set_timeout($sockets[1], 0);

    try {
        // $this->ftp is an FtpConnection
        $get = $this->ftp->get_non_blocking($this->path, $sockets[0]);

        while(!$get->is_finished()) {
            $contents = stream_get_contents($sockets[1]);

            if($contents !== false) {
                echo $contents;
                flush();
            }

            $get->resume();
        }

        $contents = stream_get_contents($sockets[1]);

        if($contents !== false) {
            echo $contents;
            flush();
        }
    } catch(Exception $e) {
        fclose($sockets[0]);    // wtb finally
        fclose($sockets[1]);

        throw $e;
    }

    fclose($sockets[0]);
    fclose($sockets[1]);
}

// class FtpConnection
public function get_non_blocking($path, $stream) {
    // $this->ftp is the FTP resource returned by ftp_connect
    return new FtpNonBlockingRequest($this->ftp, $path, $stream);
}

/* TODO Error handling. */
class FtpNonBlockingRequest {
    protected $ftp = NULL;
    protected $status = NULL;

    public function __construct($ftp, $path, $stream) {
        $this->ftp = $ftp;

        $this->status = ftp_nb_fget($this->ftp, $stream, $path, FTP_BINARY);
    }

    public function is_finished() {
        return $this->status !== FTP_MOREDATA;
    }

    public function resume() {
        if($this->is_finished()) {
            throw BadMethodCallException('Cannot continue download; already finished');
        }

        $this->status = ftp_nb_continue($this->ftp);
    }
}

相关文章