php to save pictures to the MySQL database and read pictures from the database method source codeCategory: Website 2012-03-11 15:25 5059 People read comments (0) favorite reports Database Mysqlphpsql Serverquerydatabase
In general, the picture is saved to the server, and then read out according to the path, but sometimes for security and copyright considerations, will save the picture to the MySQL database, and then read it out, such a picture right-click Properties, is not see the image address. Under the carefree lifeIt explains how to use PHP to store images in MySQL and how to read them.
MySQL database uses specialized fields to hold large volumes of data, with a blob of data type.
CREATE TABLE Images (picnum int not NULL auto_increment PRIMARY KEY, Image BLOB),
write the upload script
about how to write file uploads, We are not here to introduce, interested readers can refer to the "Web Pottery Bar" in the relevant articles. Now, let's take a look at how to receive the uploaded file and deposit it into the MySQL database. The specific script code is as follows, where we assume that the file upload domain name is picture.
- <?
- If ($Picture! = "None") {
- $PSize = filesize ($Picture);
- $mysqlPicture = addslashes (fread (fopen ($Picture, "R"), $PSize));
- Mysql_connect ($host,$username,$password 2003) or Die ("unable-to-connect to SQL server");
- @mysql_select_db ($db) or Die ("Unable to select database");
- mysql_query ("INSERT into Images (Image) VALUES ($mysqlPicture)") or Die ("Cant Perform query");
- }Else {
- echo "You do not upload no picture";
- }
- ?>
In this way, we can successfully save the image to the database. If there is a problem with inserting a picture into MySQL, you can detect the maximum size of packets allowed by the MySQL database. If the setting value is too small, we will find the corresponding record in the error log of the database.
Below, let's briefly explain the above script. First, we detect if a file has been uploaded through "if ($Picture! =" None "). Then, use the addslashes () function to avoid data formatting errors. Finally, connect to MySQL, select the database and insert the picture.
Show pictures
In knowing how to put the graph? Into the database, we need to consider how we can take pictures out of the database and display them in the HTML page. The process is a little more complicated, so let's take a look at the writing process.
Because the PHP display image needs to send the corresponding header, so we will face the problem, that is, only one picture can be displayed at a time, because we can not send a header after the other headers.
In order to solve this problem effectively, we have prepared two documents. Where the first file is the template for the HTML page, the location where the picture is displayed. The second file is used to actually output the file stream from the database as the SRC attribute of the tag.
The simple form of the first file can be as follows:
- <HTML> <BODY>
- <?
- Mysql_connect ($host,$username,$password 2003) or Die ("unable-to-connect to SQL server");
- @mysql_select_db ($db) or Die ("Unable to select database");
- $result =mysql_query ("select * from Images") or Die ("Cant Perform query");
- while ($row =mysql_fetch_object ($result)) {
- echo "picnum\ ">";
- }?>
- </BODY> </HTML>
second.php file as follows:
- <?&NBSP;&NBSP;&NBSP;
- $result =mysql_query (or die (
- $row =mysql_fetch_object ( $result);
- Header (
- echo $row->image;
- ?>