【发布时间】:2010-11-26 16:47:16
【问题描述】:
我正在尝试通过 HTTP 从 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);
}
$this->ftp->get 看起来像这样:
public function get($path, $stream) {
ftp_fget($this->ftp, $stream, $path, FTP_BINARY); // Line 200
}
使用这种方法,我只能将小文件发送到用户的浏览器。对于较大的文件,不会打印任何内容,并且出现致命错误(可从 Apache 日志中读取):
PHP 致命错误:第 200 行 /xxx/ftpconnection.php 中允许的内存大小为 16777216 字节已用尽(尝试分配 15994881 字节)
我尝试用php://stdout 替换php://output,但没有成功(似乎没有任何东西发送到浏览器)。
如何在将数据发送到浏览器的同时有效地从 FTP 下载?
注意:我不想使用file_get_contents('ftp://user:pass@host:port/path/to/file'); 或类似的。
【问题讨论】:
-
我也会对这个答案很感兴趣!