How to read images from MySQL database using PHP?

后端 未结 2 1557
半阙折子戏
半阙折子戏 2020-12-12 05:19

How to read images from MySQL database using PHP?

If the images are stored in a BLOB in the database, how to use the binary data that I get and turn it into an image

2条回答
  •  再見小時候
    2020-12-12 05:49

    What you should probably do, if storing images as BLOBs, is provide a URL that calls your script in a way that it can determine what image to return.

    Use that URL as the src, or background-image: url(...) and in the script, read the BLOB from the database in to a variable.

    Then output the variable after appropriate header information, telling the browser it is to receive an image, for instance:

    header('Content-Type: image/jpeg');
    

    Sending a Content-Length header and sensible information on caching/expiry would also be wise.


    NB. Having said all that, I tend to be wary of using BLOBs in databases, they tend to cripple performance. When I want to store images, I store then in some directory structure and reference them in the database in some fashion.

提交回复
热议问题