Save and output images using PHP and MySQL

Source: Internet
Author: User
Tags php3 file
Set the text or integer fields used in the database and the fields used to save images. The difference is that the data volume to be saved is different. 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 database.
The difference between the text or integer fields used in the database and the fields used to save images is that the data volume to be saved is different. 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 is that the maximum data length that can be stored is different.
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 introduce how to upload files here. For more information, see the 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, $ password) 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, 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, we will briefly describe 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 implementation 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 tag.
The simple format of the first file can be as follows:

Mysql_connect ($ host, $ username, $ password) 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 \"> ";
}?>

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. php3 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;
?>
At this point, we have finished the whole process of saving and displaying images using PHP and MySQL. The examples mentioned in this article are some of the simplest examples. you can add other functions as needed to improve the entire program.

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.