Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How To Print Image File
  • I'm trying to print image file with the following code: public function action_logo()
    {
    $this->response->set_header('Content-Type', 'image/jpg');
    $filename = 'logo.jpg';
    echo file_get_contents($filename);
    } But the browser just display a broken image icon (in Chrome) and Firefox display this message: "The image http://localhost...bla..bla cannot be displayed because it contains errors." I then try to compare the output by creating a plain php file: "logo.php" in DOCROOT. This file contains the same code as above.
    The file logo.php in DOCROOT can print the image properly. I then try to remove Content-Type header, both from controller and logo.php.
    I'm so surprised because the outputs are different.
    It looks like the output from controller is encoded so it cannot display image properly. How to solve this issue? Thank you.
  • If you want to return the image, set $this->response->body to the result of file_get_contents. Note that using $this->response is deprecated, your controller action should return a response object instead if you want to make your code future proof. In general, you should never use echo to output something in FuelPHP. Also, you might run into problems if you use zlib compression.
  • Sorry guys. It's my mistake. I'm using Notepad++ as editor and I unintentionally changed the config.php file to use ANSI encoding. And it caused the problem. So I've changed the file encoding back to UTF-8 with Notepad++. I'm sorry. Thank you very much.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion