CodeIgniter Forums
Having issues setting Content-Type header. Still coming out as text/html - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Having issues setting Content-Type header. Still coming out as text/html (/thread-34737.html)



Having issues setting Content-Type header. Still coming out as text/html - El Forum - 10-08-2010

[eluser]jaydisc[/eluser]
Hi all,

I've got a couple of methods that validate a user's access to a PDF or image, and then serve it out if access is granted.

Once I've validated their permission, I make sure the file exists and attempt to hand it out:

Code:
$this->load->helper('file');
if ($image = read_file('../content/path-to-image.jpeg'))
{
    $this->output->set_header('Content-Type: image/jpeg');
    echo $image;
}
else
{
    show_404();
    return FALSE;
}

Unfortunately, the mime type is still going out as text/html. I've tried using php's header() instead of CI's set_header() and php's readfile instead of CI's read_file, but it all seems the same.

I suspect this is a common issue due to an apache configuration parameter. Can anyone point me in the right direction?

Thanks.


Having issues setting Content-Type header. Still coming out as text/html - El Forum - 10-08-2010

[eluser]n0xie[/eluser]
Can you try it this way?
Code:
$img = fopen('../content/path-to-image.jpeg', 'r');
        if ($img)
        {
            header('Content-Type: image/jpeg');
            fpassthru($img);
        }



Having issues setting Content-Type header. Still coming out as text/html - El Forum - 10-10-2010

[eluser]jaydisc[/eluser]
Well, that works. Thank you.

But why doesn't the CI method work?


Having issues setting Content-Type header. Still coming out as text/html - El Forum - 10-20-2010

[eluser]scottelundgren[/eluser]
I believe the CI method doesn't work because the Output Class doesn't send the headers until the output is sent which according to the Output Class documentation is when execution ends. So while the fpassthru call is executing the output class hasn't sent any output yet including the set_header & won't until the calling function/controller finishes


Having issues setting Content-Type header. Still coming out as text/html - El Forum - 10-20-2010

[eluser]WanWizard[/eluser]
CI's output class only works on code that uses it. It doesn't include echo, fpassthru(), header() or other statements that generate output.

Code:
$img = fopen('../content/path-to-image.jpeg', 'r');
if ($img)
{
    // if you want to be absolutely sure, include this
    ob_end_clean();
    header('Content-Type: image/jpeg');
    fpassthru($img);
    exit;
}
works just fine here...