01.
<?
02.
03.
$Max_File_Size
= 100000;
04.
$File_Type_Allow
=
array
(
"image/bmp"
,
05.
"image/gif"
,
06.
"image/pjpeg"
,
07.
"image/jpeg"
); Server
08.
09.
function
validate_form(
$file_input
,
$file_size
,
$file_type
) {
10.
global
$Max_File_Size
,
$File_Type_Allow
;
11.
if
(
$file_input
==
"none"
) {
12.
$error
=
"ไม่มีไฟล์ให้อัพโหลด Upload"
;
13.
}
elseif
(
$file_size
>
$Max_File_Size
) {
14.
$error
=
"ขนาดไฟล์ใหญ่กว่า $Max_File_Size ไบต์"
;
15.
}
elseif
(!check_type(
$file_type
,
$File_Type_Allow
)) {
16.
$error
=
"ไม่อนุญาติให้อัพโหลด Upload"
;
17.
}
else
{
18.
$error
= false;
19.
}
20.
21.
return
$error
;
22.
}
23.
24.
function
check_type(
$type_check
) {
25.
global
$File_Type_Allow
;
26.
for
(
$i
=0;
$i
<
count
(
$File_Type_Allow
);
$i
++) {
27.
if
(
$File_Type_Allow
[
$i
] ==
$type_check
) {
28.
return
true;
29.
}
30.
}
31.
return
false;
32.
}
33.
34.
if
(
$_FILES
[
'userfile'
]){
35.
$error_msg
= validate_form(
$_FILES
[
'userfile'
],
$_FILES
[
'userfile'
][
"size"
],
$_FILES
[
'userfile'
][
"type"
]);
36.
if
(
$error_msg
) {
37.
echo
$error_msg
;
38.
}
else
{
39.
if
(
copy
(
$_FILES
[
'userfile'
][
'tmp_name'
],
$_FILES
[
'userfile'
][
'name'
])) {
40.
echo
"ไฟล์ Upload เรียบร้อย"
;
41.
}
else
{
42.
echo
"ไฟล์ Upload มีปัญหา"
;
43.
}
44.
}
45.
}
46.
?>