以下方法仅供参考,只是介绍下这一种方法而已。欢迎指正!!
前台(image.html):
1
<
html
>
2
<
head
>
3
<
title
>
上传图片
</
title
>
4
</
head
>
5
<
body
>
6
<
form
method
="post"
action
="upimage.php"
enctype
="multipart/form-data"
><
center
><
br
><
br
><
br
><
br
>
7
<
input
type
="hidden"
value
="204800"
name
="MAX_FILE_SIZE"
/>
8
File:
<
input
type
="file"
name
="imgfile"
/><
br
><
br
>
9
<
input
type
="submit"
value
="OK"
name
="submitbtn"
style
="width:100px;height:23px"
/></
center
>
10
</
form
>
11
</
body
>
12
</
html
>
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
1
<?
php
2
//
向数据库中插入图片
3
$imgfile
=
$_FILES
[
'
imgfile
'
];
4
$submitbtn
=
$_POST
[
'
submitbtn
'
];
5
if
(
$submitbtn
==
'
OK
'
and
is_array
(
$imgfile
))
6
{
7
$name
=
$imgfile
[
'
name
'
];
//
取得图片名称
8
$type
=
$imgfile
[
'
type
'
];
//
取得图片类型
9
$size
=
$imgfile
[
'
size
'
];
//
取得图片长度
10
$tmpfile
=
$imgfile
[
'
tmp_name
'
];
//
图片上传上来到临时文件的路径
11
if
(
$tmpfile
and
is_uploaded_file
(
$tmpfile
))
//
判断上传文件是否为空,文件是不是上传的文件
12
{
13
//
读取图片流
14
$file
=
fopen
(
$tmpfile
,
"
rb
"
);
15
$imgdata
=
bin2hex
(
fread
(
$file
,
$size
));
//
bin2hex()将二进制数据转换成十六进制表示
16
fclose
(
$file
);
17
18
$mysqli
=
mysql_connect
(
"
localhost
"
,
"
root
"
,
"
123456
"
);
//
连接数据库函数
19
mysql_select_db
(
"
test
"
);
//
选择数据库
20
//插入出数据库语句,图片数据前要加上0x,用于表示16进制数
21
if
(
mysql_query
(
"
insert into images(name,type,image) values('
"
.
$name
.
"
','
"
.
$type
.
"
',0x
"
.
$imgdata
.
"
)
"
))
22
echo
"
<center>插入成功!<br><br><a href='disimage.php'>显示图片</a></center>
"
;
23
else
24
echo
"
<center>插入失败!</center>
"
;
25
mysql_close
();
26
}
27
else
28
echo
"
<center>请先选择图片!<br><br><a href='image.html'>点此返回</a></center>
"
;
29
}
30
else
31
echo
"
<center>请先选择图片!<br><br><a href='image.html'>点此返回</a></center>
"
;
32
?>
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
11
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
12
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
13
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
14
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
15
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
16
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
17
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
18
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
19
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
20
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
21
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
22
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
23
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
24
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
25
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
26
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
27
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
28
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
29
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
30
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
31
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
32
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
显示图片(disimage.php):
1
<?
php
2
mysql_connect
(
"
localhost
"
,
"
root
"
,
"
123456
"
);
3
mysql_select_db
(
"
test
"
);
4
//
显示最新插入的那张图片
5
$result
=
mysql_query
(
"
select image from images where id=(select max(id) from images)
"
);
6
$row
=
mysql_fetch_object
(
$result
);
7
header
(
"
Content-Type:image/pjpeg
"
);
8
echo
$row
->
image;
9
mysql_close
();
10
?>
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
2
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
3
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
4
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
5
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
6
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
7
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
8
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
9
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)
10
![](https://i-blog.csdnimg.cn/blog_migrate/e40e6c90c2e98ebcf9bf1e6c592f3892.gif)