以下方法仅供参考,只是介绍下这一种方法而已。欢迎指正!!
前台(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
>

2

3

4

5

6

7

8

9

10

11

12

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
?>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

显示图片(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
?>

2

3

4

5

6

7

8

9

10
