Welcome Guest, Not a member yet? Register   Sign In
Serving an Image with HTTP Headers; white space bug. [SOLVED]
#1

[eluser]Ian Jones[/eluser]
I've spent maybe 8+ hours on this one problem, starting to not see the light at the end of the tunnel anymore! I would greatly appreciate any help.

I have an image uploads folder below PUBLIC_HTML web root, for the sake of security. I want to retrieve an image from this, to display in a view. Actually to make it even simpler I just want the browser to receive the correct HTTP headers, and the image bytes. But it doesn't work! Why? because there is a single leading whitespace at the start of the image bytes whenever I try to output from my controller. Here's my setup:

Code:
<?php
class Images extends Controller
{
    function Images()
    {
        parent::Controller();
    }
    
    function _remap()
    {
        if($filename = $this->uri->segment(2))
        {
            //$config['uri_protocol']  = "REQUEST_URI"; adjusted to remove a php problem where the filename . was being converted to a _
        
            $image = APPPATH . 'users/uploads/' . $filename;
            
            header('Content-Type: ' . get_mime_by_extension($image));
            header('Accept-Ranges: bytes');
            header('Content-Length: ' . filesize($image));
            //header('Content-Disposition: attachment; filename="'.$filename.'"');
            file_get_contents($image);
        }
    }
}
?>

I have no idea where the whitespace is coming from, but if I uncomment the 'attachment' header and download the file, I can simply remove the whitespace with TextEdit (OsX) and the image is fine!

How on earth do I track down the stray white space?


Messages In This Thread
Serving an Image with HTTP Headers; white space bug. [SOLVED] - by El Forum - 01-13-2010, 03:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB