Realtime output in CakePHP

251 Views Asked by At

I'd like to print the output of a program in php in "real time" (buffers are not important). The process takes a long time and having the (partial) data earlier would be very helpful.

Usually I'd use plain passthru() but this is done in CakePHP and it doesn't output anything until I do this:

$this->response->file($file, array('download' => true));
return $this->response;

If I just remove these lines and swap the exec() with a passthru() I get a MissingViewException

Error: [MissingViewException] View file "Songs/download.ctp" is missing.

And If I do this

$this->response=$out; #$out being the output of exec()
return $this->response;

I get this

2015-08-10 01:18:06 Error: Fatal Error (1): Call to a member function body() on string in [/storage/www/sonerezh/lib/Cake/Controller/Controller.php, line 960]
2015-08-10 01:18:06 Error: [InternalErrorException] Internal Server Error
Request URL: /songs/download/2307
Stack Trace:
#0 /storage/www/sonerezh/lib/Cake/Error/ErrorHandler.php(213): ErrorHandler::handleFatalError(1, 'Call to a membe...', '/storage/www/so...', 960)
#1 [internal function]: ErrorHandler::handleError(1, 'Call to a membe...', '/storage/www/so...', 960, Array)
#2 /storage/www/sonerezh/lib/Cake/Core/App.php(931): call_user_func('ErrorHandler::h...', 1, 'Call to a membe...', '/storage/www/so...', 960, Array)
#3 /storage/www/sonerezh/lib/Cake/Core/App.php(904): App::_checkFatalError()
#4 [internal function]: App::shutdown()
#5 {main}

What can I do?

1

There are 1 best solutions below

0
On

You could try this (not tested):

$this->response->body(function () {
    passthru ('./program') ;
}) ;
return $this->response ;

More information here.

Note: I assumed your were using CakePHP 3 since CakeResponse::file does not exist in CakePHP 2.