Php: How to upload images to the database _ PHP Tutorial

Source: Internet
Author: User
Php implements the method of uploading images to the database. Php: How to upload images to the database. This article mainly introduces how to upload images to the database in php, you can use php to upload images to the database by saving images in the database.

This article mainly introduces how to upload images to the database in php. you can share files with multiple servers by saving images in the database, which is of great practical value, for more information, see

Php implements the method of uploading images to the database. Share it with you for your reference. The specific analysis is as follows:

Php uploads images. Generally, the move_uploaded_file method is used to store images on the server. However, if a website has multiple servers, you need to publish images to all servers for normal use (except for image servers)
If you save image data to a database, multiple servers can share files to save space.

First, the image file is binary data, so you need to save the binary data in the mysql database.
Mysql database provides the BLOB type for storing a large amount of data. BLOB is a binary object that can accommodate data of different sizes.

There are four types of BLOB, except the maximum amount of information stored is different, the others are the same. You can use different types as needed.

TinyBlob Max. 255B
Blob Max 65 K
MediumBlob up to 16 MB
Up to 4 GB LongBlob

The data table photo is used to save image data. its structure is as follows:

The code is as follows:

Create table 'photo '(
'Id' int (10) unsigned not null auto_increment,
'Type' varchar (100) not null,
'Binarydata' mediumblob not null,
Primary key ('id ')
) ENGINE = MyISAM default charset = latin1 AUTO_INCREMENT = 1;

Upload_image_todb.php:

The code is as follows:

// Connect to the database
$ Conn = @ mysql_connect ("localhost", "root", "") or die (mysql_error ());
@ Mysql_select_db ('demo', $ conn) or die (mysql_error ());

// Determine action
$ Action = isset ($ _ REQUEST ['action'])? $ _ REQUEST ['action']: '';

// Upload an image
If ($ action = 'ADD '){
$ Image = mysql_escape_string (file_get_contents ($ _ FILES ['photo'] ['tmp _ name']);
$ Type = $ _ FILES ['photo'] ['type'];
$ Sqlstr = "insert into photo (type, binarydata) values ('". $ type. "', '". $ image ."')";
@ Mysql_query ($ sqlstr) or die (mysql_error ());
Header ('Location: upload_image_todb.php ');
Exit ();
// Display the image
} Elseif ($ action = 'show '){
$ Id = isset ($ _ GET ['id'])? Intval ($ _ GET ['id']): 0;
$ Sqlstr = "select * from photo where id = $ id ";
$ Query = mysql_query ($ sqlstr) or die (mysql_error ());
$ Thread = mysql_fetch_assoc ($ query );
If ($ thread ){
Header ('content-type: '. $ thread ['type']);
Echo $ thread ['binarydata'];
Exit ();
}
} Else {
// Display the image list and upload form
?>




Upload image to db demo





$ Sqlstr = "select * from photo order by id desc ";
$ Query = mysql_query ($ sqlstr) or die (mysql_error ());
$ Result = array ();
While ($ thread = mysql_fetch_assoc ($ query )){
$ Result [] = $ thread;
}
Foreach ($ result as $ val ){
Echo'

';
}
?>


}
?>

I hope this article will help you with php programming.

This article describes how to upload images to the database in php. you can save images to multiple servers in the database...

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.