Output an Image in PHP

前端 未结 12 1134
萌比男神i
萌比男神i 2020-11-22 14:33

I have an image $file ( eg ../image.jpg )

which has a mime type $type

How can I output it to the browser?

12条回答
  •  逝去的感伤
    2020-11-22 15:03

    For the next guy or gal hitting this problem, here's what worked for me:

    ob_start();
    header('Content-Type: '.$mimetype);
    ob_end_clean();
    $fp = fopen($fullyQualifiedFilepath, 'rb');
    fpassthru($fp);
    exit;
    

    You need all of that, and only that. If your mimetype varies, have a look at PHP's mime_content_type($filepath)

提交回复
热议问题