The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Best practice to return images

Stefan's Avatar

Stefan

27 Jan, 2013 06:12 PM

Hi,

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()
    {
    $request=slim::getInstance()->request();
    $update=json_decode($request->getBody());
            $id=$request->params('id');
    $sql="select * from document where id=:id";
    try
    {
    $db=getConnection();
            $stmt=$db->prepare($sql);
            $stmt->bindparam("id",$id);
            $stmt->execute();
    /*$file=$_FILES['document'];
    $name1=$file['name'];
    $path=$_SERVER['DOCUMENT_ROOT'].'/armaantestproject/';
    $path2='image/';
    $path1=$path.$path2.basename($name1);
    $image=file_get_contents("$path1");
    $response->write($image);
    return $response->withHeader('Content-Type', FILEINFO_MIME_TYPE);
    */
            foreach($data as $data)
    {
            echo json_encode(array(
    "image"=>$data['document']));
    } }
    catch(PDOException $e)
    {
    echo'{"error":"text3":'.$e->getMessage().'}}';
    }
    }

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? 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