php上传到数据库 php上传到数据库的过程( 三 )


mysql数据库提供了BLOB类型用于存储大量数据,BLOB是一个二进制对象,能容纳不同大小的数据 。
BLOB类型有以下四种,除存储的最大信息量不同外,其他都是一样的 。可根据需要使用不同的类型 。
TinyBlob最大 255B
Blob最大 65K
MediumBlob最大 16M
LongBlob最大 4G
数据表photo,用于保存图片数据,结构如下:
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代码如下:
?php
// 连接数据库
$conn=@mysql_connect("localhost","root","")or die(mysql_error());
@mysql_select_db('demo',$conn) or die(mysql_error()); // 判断action
$action = isset($_REQUEST['action'])? $_REQUEST['action'] : '';
// 上传图片
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();
// 显示图片
}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{
// 显示图片列表及上传表单
?
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""
html
head
meta http-equiv="content-type" content="text/html; charset=utf-8"
title upload image to db demo /title
/head
body
form name="form1" method="post" action="upload_image_todb.php" enctype="multipart/form-data"
p图片:input type="file" name="photo"/p
pinput type="hidden" name="action" value="https://www.04ip.com/post/add"input type="submit" name="b1" value="https://www.04ip.com/post/提交"/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 'pimg
src="/images/defaultpic.gif"
width="150"/p';
}
?
/body
/html
?php
}
?
程序运行截图和数据库截图:
php上传到数据库的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php上传到数据库的过程、php上传到数据库的信息别忘了在本站进行查找喔 。

推荐阅读