PHP saves the image to the MySQL database and the method of reading the image from the database.

Source: Internet
Author: User
Tags php3 file vars

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.

  1. <?
  2. If ($Picture! = "None") {
  3. $PSize = filesize ($Picture);
  4. $mysqlPicture = addslashes (fread (fopen ($Picture, "R"), $PSize));
  5. Mysql_connect ($host,$username,$password 2003) or Die ("unable-to-connect to SQL server");
  6. @mysql_select_db ($db) or Die ("Unable to select database");
  7. mysql_query ("INSERT into Images (Image) VALUES ($mysqlPicture)") or Die ("Cant Perform query");
  8. }Else {
  9. echo "You do not upload no picture";
  10. }
  11. ?>



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:

  1. <HTML> <BODY>
  2. <?
  3. Mysql_connect ($host,$username,$password 2003) or Die ("unable-to-connect to SQL server");
  4. @mysql_select_db ($db) or Die ("Unable to select database");
  5. $result =mysql_query ("select * from Images") or Die ("Cant Perform query");
  6. while ($row =mysql_fetch_object ($result)) {
  7. echo "picnum\ ">";
  8. }?>
  9. </BODY> </HTML>


second.php file as follows:

      1. <?&NBSP;&NBSP;&NBSP;
      2. $result =mysql_query (or die (
      3. $row =mysql_fetch_object ( $result);    
      4. Header ( 
      5. echo  $row->image;   
      6. ?>  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.