php上传图片数据库 php图片存入数据库( 二 )


$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实现上传图片到数据库并显示输出php上传图片数据库的方法 。分享给大家供大家参考php上传图片数据库,具体如下php上传图片数据库:
1.
创建数据表
CREATE
TABLE
ccs_image
(
id
int(4)
unsigned
NOT
NULL
auto_increment,
description
varchar(250)
default
NULL,
bin_data
longblob,
filename
varchar(50)
default
NULL,
filesize
varchar(50)
default
NULL,
filetype
varchar(50)
default
NULL,
PRIMARY
KEY
(id)
)engine=myisam
DEFAULT
charset=utf8
2.
用于上传图片到服务器的页面
upimage.html
!doctype
html
html
lang="en"
head
meta
charset="UTF-8"
meta
name="viewport"
content="width=device-width,
user-scalable=no,
initial-scale=1.0,
maximum-scale=1.0,
minimum-scale=1.0"
meta
http-equiv="X-UA-Compatible"
content="ie=edge"
style
type="text/css"
*{margin:
1%}
/style
titleDocument/title
/head
body
form
method="post"
action="upimage.php"
enctype="multipart/form-data"
描述:
input
type="text"
name="form_description"
size="40"
input
type="hidden"
name="MAX_FILE_SIZE"
value="https://www.04ip.com/post/1000000"
br
上传文件到数据库:
input
type="file"
name="form_data"
size="40"br
input
type="submit"
name="submit"
value="https://www.04ip.com/post/submit"
/form
/body
/html
3.
处理图片上传的php
upimage.php
?php
if
(isset($_POST['submit']))

推荐阅读