Is there a way to have a Codeigniter controller return an image?

后端 未结 7 1305
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 01:59

I was wondering if there was a way for a controller to, instead of returning a string, or a view, return an image (be it JPG, PNG etc). For example, instead of ending with a $t

7条回答
  •  青春惊慌失措
    2021-02-08 02:41

    sure you can, use this instead of $this->load->view()

    $filename="/path/to/file.jpg"; //<-- specify the image  file
    if(file_exists($filename)){ 
      $mime = mime_content_type($filename); //<-- detect file type
      header('Content-Length: '.filesize($filename)); //<-- sends filesize header
      header("Content-Type: $mime"); //<-- send mime-type header
      header('Content-Disposition: inline; filename="'.$filename.'";'); //<-- sends filename header
      readfile($filename); //<--reads and outputs the file onto the output buffer
      die(); //<--cleanup
      exit; //and exit
    }
    

提交回复
热议问题