PHP saves images to the MySQL database and read images from the database source code

Source: Internet
Author: User
Tags php3 file

Generally, images are saved to the server and read from the server according to the path. However, for security and copyright considerations, images are stored in the MySQL database and then read, you cannot see the image address when you right-click the image. The following describes how to store images in MySQL using PHP and how to read them.
The MySQL database uses special fields to store large data volumes. The data type is blob.
MySQL database defines blob as follows: BLOB data type is a large binary object that can save a variable amount of data. Blob has four types: tinyblob, blob, mediumblob, and longblob. The difference lies in the maximum data length that can be saved respectively.
After introducing the required data types, we can use the following statement to create a data table for storing images.
Create Table images (picnum int not null auto_increment primary key, image BLOB );
Write upload script
We will not discuss how to compile file uploads here. For interested readers, refer to related articles in taoba. Now, let's take a look at how to receive the uploaded files and store them in the MySQL database. The specific script code is as follows. We assume that the name of the file upload domain is picture.

<? If($Picture != "none") { $PSize = filesize($Picture); $mysqlPicture = addslashes(fread(fopen($Picture, "r"), $PSize)); mysql_connect($host,$username,$password2003) 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 did not upload any picture"; } ?> 


In this way, we can successfully Save the image to the database. If an error occurs when inserting an image into MySQL, you can check the maximum data packet size allowed by the MySQL database. If the value is too small, we will find the corresponding records in the Database Error Log.
Next, let's briefly explain the above script program. First, we use "if ($ picture! = "NONE") "to check whether a file has been uploaded. Then, use the addslashes () function to avoid data format errors. Connect to MySQL, select a database, and insert an image.
Show Image
After learning how to change the graph? After entering the database, we need to consider how to retrieve images from the database and display them on the HTML page. This process is a little more complex. Next we will introduce the compilation process.
Because PHP needs to send corresponding headers to display images, we are faced with the problem that only one image can be displayed at a time, because we cannot send other headers after the header is sent.
To effectively solve this problem, we have written two files. The first file is used as the HTML page template to locate the position where the image is displayed. The second file is used to output the file stream from the database as the src attribute of the label.
The simple format of the first file can be as follows:

<HTML> <BODY> <? mysql_connect($host,$username,$password2003) 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> 


When an HTML page is browsed, the second. php3 file is called every time an image is displayed. When the second file is called, the corresponding picture ID is passed in. We can retrieve the corresponding image from the database and display it.
The second. php file is as follows:

<? $result=mysql_query("SELECT * FROM Images WHERE PicNum=$PicNum") or die("Cant perform Query"); $row=mysql_fetch_object($result); Header( "Content-type: image/gif");echo $row->Image; ?> 


Original article: http://www.xiaoyaolife.com/Article/150169.html

A simple example of the intermediary above, you can complete it yourself, and add some features you need to make this function more complete.

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.