Monday 3 September 2018

PHP header ('Content-Type: image / jpeg') works on localhost but does not work on a real server

I want to hide the image url from users, so I am using php header() function, the problem raises when I host my website on Bluehost, it works fine on localhost. here is my function to access the image.

function download($name = '', $tiny = '0') {
    if ($name != '') {
        $file = $this->mdl->get_file($name);
        if($file){
                $mime = mime_content_type('./'.$file->path.$file->name);
                header('Content-Type: '.$mime);
                echo file_get_contents('./'.$file->path.$file->name);
        }
    }
}

here is get_file function on model:
function get_file($name=''){
    if($name!=''){
        return $this->db->select('*')
        ->from('docs')
        ->where('name',$name)
        ->get()
        ->row();
    }
    return false;
}

any helpfull answer is appretiated.

Basically this type of problem occur when any space or new line exist out of the php tags or anything printed before the header function.
  • Try this :
ob_clean(); before the header

0 comments:

Post a Comment