ตารางเก็บข้อมูล
Code (PHP)
1.
CREATE TABLE [dbo].[Images](
2.
[Img_Id] [int] IDENTITY(1,1) NOT NULL,
3.
[Img_Name] [varchar](50) NOT NULL,
4.
[Img_Size] [varchar](50) NOT NULL,
5.
[Img_Data] [image] NOT NULL,
6.
[Img_Type] [varchar](50) NOT NULL
เพิ่ม Text Limit และ Text Size ใน C:\Windows\php.ini
php.ini
1.
; Valid range 0 - 2147483647. Default = 4096.
2.
mssql.textlimit = 2147483647
3.
4.
; Valid range 0 - 2147483647. Default = 4096.
5.
mssql.textsize = 2147483647
Code (img_add.php)
01.
<html>
02.
<head>
03.
<title>Untitled Document</title>
04.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=windows-874"
>
05.
</head>
06.
07.
<body>
08.
<form name=
"form1"
method=
"post"
action=
"img_insert.php"
enctype=
"multipart/form-data"
>
09.
<input type=
"file"
name=
"upfile"
>
10.
<input type=
"submit"
name=
"Submit"
value=
"Submit"
>
11.
</form>
12.
</body>
13.
</html>
Code (img_insert.php )
01.
<?
02.
if
(
$_FILES
[
"upfile"
][
"name"
] !=
"Please Insert Images"
)
03.
{
04.
$FileName
=
$_FILES
[
'upfile'
][
'tmp_name'
];
05.
$Name
=
$_FILES
[
"upfile"
][
"name"
];
06.
$Size
=
$_FILES
[
"upfile"
][
"size"
];
07.
$Type
=
$_FILES
[
"upfile"
][
"type"
];
08.
$DataImage
=
file_get_contents
(
$FileName
);
09.
$ArrData
= unpack(
"H*hex"
,
$DataImage
);
10.
$HexData
=
"0x"
.
$ArrData
[
'hex'
];
11.
12.
//*** Insert Record ***//
13.
$Connect
= mssql_connect(
"yourhost"
,
"user"
,
"pass"
)
or
die
(
"Error Connect to DB"
);
14.
$DB
= mssql_select_db(
"Database"
);
15.
$SQL
=
"INSERT INTO Images "
;
16.
$SQL
.="(Img_Name,Img_Size,Img_Data,Img_Type)
17.
VALUES (
'$Name'
,
'$Size'
,
$HexData
,
'$Type'
)";
18.
$Query
= mssql_query(
$SQL
)
or
die
(
"error "
);
19.
20.
echo
"Upload Complete<br>"
;
21.
echo
"<a href='Img_show.php'>View_img </a> "
;
22.
}
23.
?>
Code (img_show.php)
01.
<?
02.
$Connect
= mssql_connect(
"yourhost"
,
"user"
,
"pass"
)
or
die
(
"Error Connect to DB"
);
03.
$DB
= mssql_select_db(
"Database"
);
04.
$SQL
=
"SELECT * FROM Images"
;
05.
$Query
= mssql_query(
$SQL
)
or
die
(
"Error Query ["
.
$SQL
.
"]"
);
06.
?>
07.
<table width=
"550"
border=
"1"
cellpadding=
"0"
cellspacing=
"0"
>
08.
<tr>
09.
<th width=
"50"
> <div align=
"center"
>Img_Id</div></th>
10.
<th width=
"150"
> <div align=
"center"
>Image</div></th>
11.
<th width=
"150"
> <div align=
"center"
>Img_Name</div></th>
12.
<th width=
"150"
>Img_Size</th>
13.
<th width=
"150"
>Img_Type</th>
14.
</tr>
15.
<?
while
(
$objResult
= mssql_fetch_array(
$Query
)) { ?>
16.
<tr>
17.
<td><div align=
"center"
><?=
$objResult
[
"Img_Id"
];?></div></td>
18.
<td><center><img src=
"img_view.php?Img_Id=<?=$objResult["
Img_Id
"];?>"
></center></td>
19.
<td><center><?=
$objResult
[
"Img_Name"
];?></center></td>
20.
<td><div align=
"center"
><?=
$objResult
[
"Img_Size"
];?></div></td>
21.
<td><div align=
"center"
> <?=
$objResult
[
"Img_Type"
];?></div></td>
22.
</tr>
23.
<? } ?>
24.
</table>
25.
<?
26.
mssql_close(
$Connect
);
27.
?>
Code (img_view.php)
01.
<?
02.
03.
$Connect
= mssql_connect(
"yourhost"
,
"user"
,
"pass"
)
or
die
(
"Error Connect to DB"
);
04.
$DB
= mssql_select_db(
"Database"
);
05.
$SQL
=
"SELECT * FROM Images WHERE Img_Id = '$Img_Id' "
;
06.
$Query
= mssql_query(
$SQL
)
or
die
(
"Error Query ["
.
$SQL
.
"]"
);
07.
$Result
= mssql_fetch_array(
$Query
);
08.
$Type
=
$Result
[
"Img_Type"
];
09.
header(
"Content-type: $type "
);
10.
echo
$Result
[
"Img_Data"
];
11.
?>
Screenshot
No comments:
Post a Comment