流FTP下载到输出

str*_*ger 9 php ftp

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

这是我到目前为止:

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);                                   
}                                                    
Run Code Online (Sandbox Code Playgroud)

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

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

通过这种方法,我只能将小文件发送到用户的浏览器.对于较大的文件,没有任何内容被打印,我得到一个致命的错误(可从Apache日志中读取):

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

我试图取代php://outputphp://stdout没有成功(似乎没有任何发送到浏览器).

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

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

str*_*ger 8

找到了解决方案!

创建套接字对(匿名管道?).使用非阻塞ftp_nb_fget功能写入管道的一端和管道echo的另一端.

测试速度很快(在100Mbps连接上容易10MB/s)因此没有太多的I/O开销.

务必清除任何输出缓冲区.框架通常会缓冲输出.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Cia*_*lty 5

尝试:

@readfile('ftp://username:password@host/path/file'));
Run Code Online (Sandbox Code Playgroud)

我发现有很多文件操作,值得让底层操作系统功能为您处理.

  • 有没有办法逃避用户名和密码,所以如果它们包含像`@`或`/`这样的字符,它会正确读取? (2认同)