用PHP存储和显示来自MySQL的图像 [英] Storing and displaying images from MySQL with PHP
问题描述
如何通过从HTML表单发送图像将图像文件存储在MySQL中的PHP中?我只知道MySQL和HTML部分。
以下是HTML表单:
< form method =postenctype =multipart / form-dataaction =insert_image.php>
< input type =filename =image/>
< input type =submit/>
< / form>
我知道如何连接到数据库并存储正常信息,但是如何正确解析数据将图像文件存储到MySQL BLOB字段?而且我该如何从MySQL显示它?
ps:使用PDO进行数据库连接的Im
<?php
#获取上传的图像并存储它
if(isset($ _ FILES ['image'] ['tmp_name'])){
//打开mysqli db连接
$ mysqli = new mysqli($ mysqliHost,$ mysqliUsername,$ mysqliPassword,$ mysqliDatabase);
//获取图像数据
$ binary = file_get_contents($ _ FILES ['image'] ['tmp_name']);
//获取mime类型
$ finfo = new finfo(FILEINFO_MIME);
$ type = $ finfo-> file($ _ FILES ['image'] ['tmp_name']);
$ mime = substr($ type,0,strpos($ type,';'));
$ query =INSERT INTO`images`('data`,`mime`,`name`)VALUES('。$ mysqli-> real_escape_string($ binary)。'','' $ mysqli-> real_escape_string($ MIME) '' $ mysqli-> real_escape_string($ _ FILES [ '图像'] [ '名称']) ');
$ mysqli-> query($ query);
查看上传图片
if(isset($ _ GET ['imageName'])){
//打开mysqli db连接
$ mysqli = new mysqli($ mysqliHost,$ mysqliUsername,$ mysqliPassword,$ mysqliDatabase);
//查询db中的图像
$ query =SELECT`data`,`mime` FROM` images` WHERE`name` ='。$ mysqli-> real_escape_string($ _ GET [ 'imageName']) '。
$ result = $ mysql-> query($ query);
$ b $ if($ result-> num_rows){
//从db中获取查询结果select
$ assoc = $ result-> fetch_assoc( );
//让客户端浏览器知道你要发送什么类型的数据
header('Content-type:'。$ assoc ['mime']);
//将二进制数据转储到浏览器
echo $ assoc ['data'];
出口;
} else {
header('HTTP / 1.1 404 Not Found');
出口;
}
}
?>
我的脚本没有考虑具有相同名称的图像,您可以换出它所表示的部分$ _FILES ['image'] ['name']给另一个为其创建唯一名称的变量,或者使用插入的ID(PRIMARY AUTO_INCREMENT MySQL key)。
< p $>
CREATE TABLE`images`(
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`data` longblob NOT NULL,
`mime` varchar(50)NOT NULL,
`name` varchar(255)NOT NULL,
PRIMARY KEY (`id`),
KEY`name`(`name`)
)ENGINE = MyISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1;
How can I store a image file in MySQL with PHP by sending the image from a HTML form? I only know the MySQL and HTML part of the stuff.
Here's the HTML form:
<form method="post" enctype="multipart/form-data" action="insert_image.php">
<input type="file" name="image" />
<input type="submit" />
</form>
I know how to connect to the database and store normal information, but how can I parse the data correctly to store the image file to a MySQL BLOB field? And also how can I display it from MySQL?
ps: Im using PDO to do the database connections.
<?php
# getting the uploaded image and storing it
if ( isset($_FILES['image']['tmp_name']) ) {
// open mysqli db connection
$mysqli = new mysqli($mysqliHost,$mysqliUsername,$mysqliPassword,$mysqliDatabase);
// get image data
$binary = file_get_contents($_FILES['image']['tmp_name']);
// get mime type
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->file($_FILES['image']['tmp_name']);
$mime = substr($type, 0, strpos($type, ';'));
$query = "INSERT INTO `images` (`data`,`mime`,`name`) VALUES('".$mysqli->real_escape_string($binary)."','".$mysqli->real_escape_string($mime)."','".$mysqli->real_escape_string($_FILES['image']['name'])."')";
$mysqli->query($query);
}
# viewing the uploaded image
if ( isset($_GET['imageName']) ) {
// open mysqli db connection
$mysqli = new mysqli($mysqliHost,$mysqliUsername,$mysqliPassword,$mysqliDatabase);
// query for the image in the db
$query = "SELECT `data`,`mime` FROM `images` WHERE `name`='".$mysqli->real_escape_string($_GET['imageName'])."'";
$result = $mysql->query($query);
if ( $result->num_rows ) {
// grab the query result from the db select
$assoc = $result->fetch_assoc();
// let the client browser know what type of data you're sending
header('Content-type: '.$assoc['mime']);
// dump the binary data to the browser
echo $assoc['data'];
exit;
} else {
header('HTTP/1.1 404 Not Found');
exit;
}
}
?>
My script does not account for images with the same name, you can swap out the part where it says $_FILES['image']['name'] to another variable that has/creates a unique name for it, or use the inserted ID (PRIMARY AUTO_INCREMENT MySQL key).
Here is a sample table schema:
CREATE TABLE `images` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`data` longblob NOT NULL,
`mime` varchar(50) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
这篇关于用PHP存储和显示来自MySQL的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!