Welcome Guest, Not a member yet? Register   Sign In
Having issues setting Content-Type header. Still coming out as text/html
#1

[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.
#2

[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);
        }
#3

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

But why doesn't the CI method work?
#4

[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
#5

[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...




Theme © iAndrew 2016 - Forum software by © MyBB