1

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

rtom
  • 585
  • 1
  • 12
  • 26
  • Show us code otherwise all I can see is a duplicate of something like this http://stackoverflow.com/questions/10938822/how-do-i-assign-content-from-readfile-into-a-variable – Scuzzy Apr 20 '17 at 10:48
  • 1
    I edited it a bit, hope that's enough, because it's few hundreads line of code otherwise. – rtom Apr 20 '17 at 11:04
  • Without echoing the buffer content, have you reviewed what is actually downloaded at the end of script execution? My thoughts is you're getting a header conflict. http://stackoverflow.com/questions/3111179/how-do-headers-work-with-output-buffering-in-php – Scuzzy Apr 20 '17 at 12:36

1 Answers1

0

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');
Scuzzy
  • 12,186
  • 1
  • 46
  • 46