The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Best practice to return images

Stefan's Avatar


27 Jan, 2013 06:12 PM


I'm searching for the best way to return files with slim. I got this:

        $image = file_get_contents('images/e979d2a92be68c23bb9f776046d696ab.jpg');
        $app->response()->header('Content-Type', 'content-type: image/jpg');
        echo $image;
and it works. Is there for example a possibility to detect the content type automatically?

Best regards ...

  1. 1 Posted by Bob Richardson on 06 Jun, 2014 02:32 PM

    Bob Richardson's Avatar

    Old topic, probably long-resolved, but for anyone coming across this issue like myself:

    $image = file_get_contents("images/e979d2a92be68c23bb9f776046d696ab.jpg");
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $app->response->header('Content-Type', 'content-type: ' . $finfo->buffer($image));
    echo $image;

    Works a charm.

  2. 2 Posted by armaan on 25 Jun, 2018 04:41 AM

    armaan's Avatar

    plls give me right code.i have wrtten this code

    function display()
    $sql="select * from document where id=:id";
    return $response->withHeader('Content-Type', FILEINFO_MIME_TYPE);
            foreach($data as $data)
            echo json_encode(array(
    } }
    catch(PDOException $e)

  3. Josh Lockhart closed this discussion on 12 Feb, 2021 07:37 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac