CodeIgniter Forums
Random problem with set_header() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Random problem with set_header() (/showthread.php?tid=75381)



Random problem with set_header() - mdr1384 - 01-31-2020

I am having a random issue with Content-Disposition header not being sent from my controller, depending on the content.  Here is my controller function:


PHP Code:
public function download() {
        $this->output->set_content_type('text/csv');
        $this->output->set_header('Content-Disposition: attachment; filename="test.csv"');
        //header('Content-Disposition: attachment; filename="test.csv"');
        readfile('/tmp/test.csv');




If I un-comment the header() call, then it always works, but otherwise sometimes it works and sometimes not, seems to depend either on the content of /tmp/test.csv or else it is just plain random -- I have not been able to nail down any specific lines of content that consistently reproduce the issue.

It seems pretty simple but maybe I'm doing something wrong?

(This is CI 3.1.10, using PHP 7.3.11, with Apache 2.4 and modsecurity2)


RE: Random problem with set_header() - jreklund - 01-31-2020

I would use the built in force_download in Codeigniter for this: https://codeigniter.com/user_guide/helpers/download_helper.html


RE: Random problem with set_header() - mdr1384 - 01-31-2020

That works - and (bonus) I looked at the code for force_download() and it outputs in 1MB chunks so as to not overrun memory, which I was concerned about.

So thanks!

(But it would be nice to know what the heck is wrong with my example)