Http://www.jb51.net/article/61034.htm proud snow star Maple font: [Increase decrease] Type: Reproduced this article mainly introduces the PHP implementation of the upload image to the database method, you can save the picture in the database to implement multiple servers to share the file function, Very practical value, the need for friends can refer to the following
PHP implementation of the upload image to the database method. Share to everyone for your reference. The specific analysis is as follows:
PHP uploads images, generally using the Move_uploaded_file method saved on the server. However, if a website has more than one server, you will need to publish the pictures to all the servers in order to work properly (except using the image server)
If you save picture data to a database, you can save space by sharing files between multiple servers.
First, the picture 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 accommodate different sizes of data.
There are four types of blobs, except that the maximum amount of information stored is different, and the others are the same. Different types can be used as needed.
Tinyblob Max 255B
Blob Max 65K
Mediumblob Max 16M
Longblob Max 4G
The data sheet photo, which is used to save the picture data, is structured as follows:
Copy CodeThe code is as follows: CREATE TABLE ' photo ' (
' id ' int (ten) 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 CodeThe code is as follows: <?php
Connecting to a database
[Email Protected]_connect ("localhost", "root", "") or Die (Mysql_error ());
@mysql_select_db (' demo ', $conn) or Die (Mysql_error ());
Judging action
$action = Isset ($_request[' action ')? $_request[' action ']: ';
Upload 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 ();
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 list of pictures 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
}
?>
PHP implementation of uploading images to the database method