流FTP下载到输出 [英] Stream FTP download to output
问题描述
我试图通过HTTP从FTP传输/管道文件到用户的浏览器。也就是说,我试图在FTP服务器上打印一个文件的内容。
这是我迄今为止的内容:
public function echo_contents(){
$ file = fopen('php:// output','w +');
if(!$ file){
抛出新的异常('Unable to open output');
}
尝试{
$ 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致命错误:允许的内存大小为16777216字节耗尽(试图分配15994881字节)在/xxx/ftpconnection.php上行200
我尝试将 php:// output
with php:// stdout
没有成功(没有任何东西似乎发送到浏览器)。
如何在从FTP同时有效地下载数据的同时将数据发送到浏览器?
注意:我不想使用 file_get_contents('ftp:// user:pass @ host:port / path / to / file');
或类似的名称。 / b>
创建一个套接字对(匿名管道?)。使用非阻塞
ftp_nb_fget
函数来写入管道的一端,而 echo
管道的另一端。$ b
被测试为 fast (在100Mbps连接上很容易达到10MB / s),所以没有太多的I / O开销。
确保清除任何输出缓冲区。框架通常会缓冲你的输出。
$ b
public function echo_contents(){
/ * FTP写入[ 0]。数据从[1]传递过来。 * /
$ sockets = stream_socket_pair(STREAM_PF_UNIX,STREAM_SOCK_STREAM,STREAM_IPPROTO_IP);
if($ sockets === FALSE){
抛出新的异常('Unable to create socket pair');
}
stream_set_write_buffer($ sockets [0],0);
stream_set_timeout($ sockets [1],0);
try {
// $ this-> ftp是一个FtpConnection
$ get = $ this-> ftp-> get_non_blocking($ this->路径, $插座[0]); ($!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是返回的FTP资源ftp_connect
返回新的FtpNonBlockingRequest($ this-> ftp,$ path,$ stream);
}
/ * TODO错误处理。 * /
class FtpNonBlockingRequest {
protected $ ftp = NULL;
保护$ status = NULL;
公共函数__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; $'
$ b public function resume(){
if($ this-> is_finished()){
throw BadMethodCallException('无法继续下载;已经完成' );
}
$ this-> status = ftp_nb_continue($ this-> 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.
This is what I have so far:
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
looks like this:
public function get($path, $stream) {
ftp_fget($this->ftp, $stream, $path, FTP_BINARY); // Line 200
}
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 Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 15994881 bytes) in /xxx/ftpconnection.php on line 200
I tried replacing php://output
with php://stdout
without success (nothing seems to be sent to the browser).
How can I efficiently download from FTP while sending that data to the browser at the same time?
Note: I would not like to use file_get_contents('ftp://user:pass@host:port/path/to/file');
or similar.
Found a solution!
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.
Tested to be fast (easily 10MB/s on a 100Mbps connection) so there's not much I/O overhead.
Be sure to clear any output buffers. Frameworks commonly buffer your output.
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);
}
}
这篇关于流FTP下载到输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!