003.
<head>
004.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=windows-874"
/>
005.
<title>Workshop 16-1</title>
006.
<link rel=
"stylesheet"
href=
"../css/style.css"
/>
007.
</head>
008.
<body>
009.
<?php
010.
@mysql_connect(
"localhost"
,
"root"
,
"leaf"
)
or
die
(mysql_error());
011.
mysql_select_db(
"phpmysql"
);
012.
013.
014.
if
(
$_FILES
) {
015.
<select name=
"group"
id=
"group"
>
016.
017.
018.
019.
020.
$num_files
=
count
(
$_FILES
[
'file'
][
'name'
]);
021.
022.
023.
for
(
$i
= 0;
$i
<
$num_files
;
$i
++) {
024.
if
(
$_FILES
[
'file'
][
'error'
][
$i
] != 0) {
025.
continue
;
026.
}
027.
028.
029.
030.
031.
032.
033.
034.
035.
036.
037.
038.
039.
040.
041.
042.
043.
044.
045.
046.
047.
048.
049.
050.
051.
052.
053.
054.
055.
056.
057.
058.
059.
060.
061.
062.
$upfile
=
$_FILES
[
'file'
][
'tmp_name'
][
$i
];
063.
$file
=
fopen
(
$upfile
,
"r"
);
064.
$content
=
fread
(
$file
,
filesize
(
$upfile
));
065.
$content
=
addslashes
(
$content
);
066.
fclose(
$file
);
067.
068.
069.
$size
=
$_FILES
[
'file'
][
'size'
][
$i
];
070.
$name
=
$_FILES
[
'file'
][
'name'
][
$i
];
071.
$type
=
$_FILES
[
'file'
][
'type'
][
$i
];
072.
073.
$sql
=
"INSERT INTO uploadfile VALUES(0, '$name', '$type', '$size', '$content');"
;
074.
$qry
= mysql_query(
$sql
);
075.
076.
077.
if
(!
$qry
) {
078.
echo
"การบันทึกไฟล์ลำดับที่ "
. (
$i
+ 1) .
" เกิดข้อผิดพลาด! <br />"
;
079.
}
080.
else
{
081.
echo
"การบันทึกไฟล์ลำดับที่ "
. (
$i
+ 1) .
" เสร็จเรียบร้อย <br />"
;
082.
}
083.
}
084.
}
085.
?>
086.
<p />
087.
<form action=
"<?php echo $_SERVER['PHP_SELF']; ?>"
method=
"post"
enctype=
"multipart/form-data"
name=
"form1"
id=
"form1"
>
088.
File 1:
089.
<label>
090.
<input name=
"file[]"
type=
"file"
id=
"file[]"
/>
091.
<br />
092.
File 2:
093.
<input name=
"file[]"
type=
"file"
id=
"file[]"
/>
094.
<br />
095.
File 3:
096.
<input name=
"file[]"
type=
"file"
id=
"file[]"
/>
097.
<br />
098.
<br />
099.
<input type=
"submit"
name=
"Submit"
value=
"Upload"
/>
100.
<br />
101.
</label>
102.
</form><p />
103.
<?php
104.
105.
106.
$result
= mysql_query(
"SELECT * FROM uploadfile;"
);
107.
if
(!
$result
|| mysql_num_rows(
$result
) == 0) {
108.
echo
"ไม่พบไฟล์ในฐานข้อมูล </body></html>"
;
109.
exit
();
110.
}
111.
112.
113.
echo
"<table border=1 cellpadding=5 cellspacing=1>
114.
<caption>รายชื่อไฟล์ที่เก็บไว้ในฐานข้อมูล</caption>";
115.
116.
117.
echo
"<tr>"
;
118.
$num_fields
= mysql_num_fields(
$result
);
119.
for
(
$i
= 0;
$i
<
$num_fields
- 1;
$i
++) {
120.
echo
"<th>"
. mysql_field_name(
$result
,
$i
) .
"</th>"
;
121.
}
122.
echo
"</tr>"
;
123.
124.
125.
while
(
$data
= mysql_fetch_array(
$result
)) {
126.
echo
"<tr valign=top>"
;
127.
128.
$id
=
$data
[0];
129.
for
(
$i
= 0;
$i
<
$num_fields
- 1;
$i
++) {
130.
echo
"<td>"
;
131.
132.
133.
if
(
$i
== 1) {
134.
echo
"<a href=\"show_file.php?id=$id\" target=\"_blank\">{$data[$i]}</a>"
;
135.
}
136.
else
{
137.
echo
$data
[
$i
];
138.
}
139.
echo
"</td>"
;
140.
}
141.
echo
"</tr>"
;
142.
}
143.
echo
"</table>"
;
144.
?>
145.
</body>
146.
</html>