PHP implementation of uploading images to the database method
This article mainly introduces the PHP implementation of the upload image to the database method, you can save the picture in the database to realize the function of multiple servers to share files, very practical value, the need for friends can refer to the next
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:
The 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:
The code is as follows:
Connecting to a database
$conn = @mysql_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
?>
<title>Upload image to DB demo</title>
$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 is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/956982.html www.bkjia.com true http://www.bkjia.com/PHPjc/956982.html techarticle PHP Implementation of the upload image to the database method this article mainly introduces the PHP implementation of uploading images saved to the database method, you can save the picture in the database to implement more than one server ...