How does php convert images into byte streams and store them in the database?

how to convert the picture into a byte stream so that you don"t have to worry about the space taken up by the file to store the picture

Php
Mar.14,2021

base64 encode


it seems to me that no one has answered this question properly. I also had this doubt when I was a beginner. Let me answer it.

first of all, do not save pictures in the database. If the field of the data stored in the database is set too large, the database data query will be very slow.

in addition, picture data, you can directly use PHP file_get_contents to get picture information, but if you want to put the picture back, you need to use file_put_contents . In addition, if you put it back, you must name the picture name. You also have to store a lot of picture-related information when you take the picture. I don't think it's necessary to do this.

of course, for images, there is also base64_encode for base64 conversion. In fact, I can tell you that the image storage space will be larger after conversion.

said the above points. Let me tell you what consideration we usually take to store pictures. Usually, we upload images to a path, such as / var/www/statics/test.jpg . What I show here is the absolute path. Generally speaking, we use the relative path or upload the image to the cloud server. If the image is uploaded to a cloud server, the image you may get is in the form of https://img.codeshelper.com/1.jpg.
that is to say, what we store is the path of the picture, whether it's the network path or the absolute path or the relative path, as long as we take the picture path out of the library and find the picture according to the path where the picture is stored, and the path data itself is very small, so we don't need to think too much about getting the original picture through the path, so no one will convert the picture data into bytes and store it in the database.

in addition, I have also seen you say that you do not need to consider the problem of taking up too much space for pictures, but I can tell you clearly here that on the one hand, we write programs to consider convenience and quickly implement certain functions, and at the same time, we also consider performance issues. The ultimate goal of our programs is to serve users, regardless of whether the user is ourselves or the real user who can bring us benefits. So, after the program can be implemented, there is another problem to consider, that is, the performance problem.

so, generally speaking, we want the data stored in the database to be as small as possible.


the database has a heavy load and also takes up server space. How do you save space?


dfdfdfds

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e3cb00-4bddf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e3cb00-4bddf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?