How can I store and retrieve images from a MySQL database using PHP?

后端 未结 6 2295
心在旅途
心在旅途 2020-11-22 05:21

How can I insert an image in MySQL and then retrieve it using PHP?

I have limited experience in either area, and I could use a little code to get me started in figur

6条回答
  •  独厮守ぢ
    2020-11-22 05:42

    Beware that serving images from DB is usually much, much much slower than serving them from disk.

    You'll be starting a PHP process, opening a DB connection, having the DB read image data from the same disk and RAM for cache as filesystem would, transferring it over few sockets and buffers and then pushing out via PHP, which by default makes it non-cacheable and adds overhead of chunked HTTP encoding.

    OTOH modern web servers can serve images with just few optimized kernel calls (memory-mapped file and that memory area passed to TCP stack), so that they don't even copy memory around and there's almost no overhead.

    That's a difference between being able to serve 20 or 2000 images in parallel on one machine.

    So don't do it unless you absolutely need transactional integrity (and actually even that can be done with just image metadata in DB and filesystem cleanup routines) and know how to improve PHP's handling of HTTP to be suitable for images.

提交回复
热议问题