PHP Implementation upload image Save to database method _php tips

Source: Internet
Author: User

PHP to implement upload images saved to the database method. Share to everyone for your reference. The specific analysis is as follows:

PHP upload pictures, generally using the Move_uploaded_file method to save on the server. However, if a Web site has more than one server, you need to publish the image to all servers to be used normally (except for the image server)
If you save the picture data to the database, multiple servers can realize file sharing and save space.

First, the image file is binary data, so you need to save the binary data in the MySQL database.
The MySQL database provides a BLOB type for storing large amounts of data, and a BLOB is a binary object that can hold data of different sizes.

The BLOB type has the following four kinds, except the maximum amount of information stored is different, the other is the same. You can use different types depending on your needs.

Tinyblob Max 255B
Blob Max 65K
Mediumblob Max 16M
Longblob Max 4G

Datasheet photo, which is used to save picture data, is structured as follows:

Copy Code code as follows:
CREATE TABLE ' photo ' (
' ID ' int (a) unsigned not NULL auto_increment,
' type ' varchar not NULL,
' BinaryData ' Mediumblob not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT charset=latin1 auto_increment=1;

upload_image_todb.php:

Copy Code code as follows:
<?php
Connecting to a database
$conn = @mysql_connect ("localhost", "root", "") or Die (Mysql_error ());
@mysql_select_db (' demo ', $conn) or Die (Mysql_error ());

Judge Action
$action = Isset ($_request[' action ')? $_request[' action ']: ';

Upload pictures
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 ();
Show pictures
}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 a picture list and upload a form
?>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> upload image to db demo </title>

<body>
<form name= "Form1" method= "Post" action= "upload_image_todb.php" enctype= "Multipart/form-data" >
<p> Pictures: <input type= "file" name= "Photo" ></p>
<p><input type= "hidden" name= "action" value= "add" ><input type= "Submit" Name= "B1" value= "Submit" ></p >
</form>

<?php
$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 ' <p></p> ';
}
?>
</body>
<?php
}
?>

I hope this article will help you with your PHP program design.

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.