Ob_start not catching output file

561 Views Asked by At

I am trying to generate report and upload it. Problem is that when function that generates the file ends with readfile(), it just downloads the file even if I put ob_start() before the function call. So I don't understand why ob_start is not catching It.

Problem should be here

  1. I call ob_start()

  2. Then I call function that outputs file like this

    header('Content-Type: application/vnd.openxmlformats-officedocument.' . 'wordprocessingml.document'); header('Content-Disposition: attachment; filename="' . $fileNameDownload . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($fileName . '.' . $this->_extension)); readfile($fileName . '.' . $this->_extension);

  3. After previous function call I put $content = ob_get_contents();

and 4. ob_end_clen()

I would expect that with readfile() the function will start to write into buffer, but instead it outputs the file for download

1

There are 1 best solutions below

0
Scuzzy On

I believe the fact that headers are being set, and not being captured by the output buffer, that you may need to cancel them out, especially since Content-Disposition: attachment is setting the response to be a forced download.

http://www.php.net/manual/en/function.ob-start.php

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

Therefore if the function sets:

header('Content-Type: application/vnd.openxmlformats-officedocument.' . 'wordprocessingml.document');
header('Content-Disposition: attachment; filename="' . $fileNameDownload . '"');

you will need to do cancel it out after you've ended the output buffering capture, ie:

header('Content-Type: text/html');
header('Content-Disposition: inline');