การ uploadsรูปภาพ ลงฐานข้อมูล mysql

 

ออกแบบฐานข้อมูลดังนี้

Create Database picture;

CREATE TABLE picture (
id int(3) NOT NULL auto_increment,
images longblob NOT NULL,
PRIMARY KEY (id)
);

ขั้นที่ 1 ทำ From เพื่อ เลือกไฟล์ที่จะ uploads

Sample1.php


<html>
<head>
<title>Un title page</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
<style type="text/css">
<!--
body { margin: 0px 0px; padding: 0px 0px}
a:link { color: #005CA2; text-decoration: none}
a:visited { color: #005CA2; text-decoration: none}
a:active { color: #0099FF; text-decoration: underline}
a:hover { color: #0099FF; text-decoration: underline}
-->
</style>
</head>
<body bgcolor="#FFFFFF">
<br>
<form method="POST" action="sample2.php" enctype ="multipart/form-data">
<font face="MS Sans Serif" size="2"> แก้ไข Logo</font>
<input type="file" name="files">
<input type="submit" name="Submit" value="uploads">
</form>
</body>
</html>


Out Put

 

ขั้นที่ 2 ออกแบบ From เพื่อ uploads ไฟล์

Sample2.php

<?
$host="localhost";
$username="";
$pass_word="";
$db="picture";
mysql_connect( $host,$username,$pass_word) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");


$filename =$HTTP_POST_FILES['files']['name'];
$filetempname =$HTTP_POST_FILES['files']['tmp_name'];
$filesize =$HTTP_POST_FILES['files']['size'];

$fp = fopen($filetempname,"r");
$data = fread($fp,filesize($filetempname));
fclose($fp);
$data = addslashes($data);


$sql="insert into picture(images) values ('$data')";
$db_query=mysql_db_query($db,$sql);
echo"Uploads รูปภาพเรียบร้อยแล้ว";

?>

Out Put

 

ขั้นที่ 3 ออกแบบ From เพื่อ ดึงรูปภาพมาแสดง

<?
$host="localhost";
$username="";
$pass_word="";
$db="picture";
mysql_connect( $host,$username,$pass_word) or die ("ติดต่อกับฐานข้อมูล Mysql ไม่ได้ ");
mysql_select_db($db) or die("เลือกฐานข้อมูลไม่ได้");

$sql = "select * from picture where id=1"; // หากต้องการดึงเฉพาะ Reccord ใด Record หนึ่ง ให้ใช้คำสั่ง where id='$ตัวแปร'
$result = mysql_query($sql) or die("ไม่สามารถ query ข้อมูลได้");

$images = mysql_fetch_array($result);
echo $images['images']

?>

Sample3.php


 


Hosted by www.Geocities.ws

1