Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > PHP SQL Server BLOB การ Upload และ Insert รูปภาพลงใน SQL Server mssql (BLOB,Image DataType)



 
Clound SSD Virtual Server

PHP SQL Server BLOB การ Upload และ Insert รูปภาพลงใน SQL Server mssql (BLOB,Image DataType)

ตัวอย่างการขียน PHP เพื่อทำการ Upload รูปภาพลงในฐานข้อมูล SQL Server ด้วย function mssql ของ SQL Server โดยการจัดเก็บข้อมูลในรูปแบบของ BLOB หรือ Image DataType และตัวอย่าง PHP กับ SQL Server การอ่านข้อมูลจาก BLOB มาแสดงผลทาง Web Brownser

ตารางเก็บข้อมูล
Code (PHP)
CREATE TABLE [dbo].[Images](
	[Img_Id] [int] IDENTITY(1,1) NOT NULL,
	[Img_Name] [varchar](50) NOT NULL,
	[Img_Size] [varchar](50) NOT NULL,
	[Img_Data] [image] NOT NULL,
	[Img_Type] [varchar](50) NOT NULL


เพิ่ม Text Limit และ Text Size ใน C:\Windows\php.ini

php.ini
; Valid range 0 - 2147483647.  Default = 4096.
mssql.textlimit = 2147483647

; Valid range 0 - 2147483647.  Default = 4096.
mssql.textsize = 2147483647


Code (img_add.php)
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
</head>

<body>
<form name="form1" method="post" action="img_insert.php" enctype="multipart/form-data" >
  <input type="file" name="upfile" >
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>



Code (img_insert.php )
<? 
if($_FILES["upfile"]["name"] != "Please Insert Images")
	{
$FileName = $_FILES['upfile']['tmp_name']; 
$Name=$_FILES["upfile"]["name"]; 
$Size=$_FILES["upfile"]["size"]; 
$Type=$_FILES["upfile"]["type"]; 
$DataImage = file_get_contents($FileName ); 
$ArrData = unpack("H*hex", $DataImage); 
$HexData = "0x".$ArrData['hex']; 

//*** Insert Record ***//
$Connect = mssql_connect("yourhost","user","pass") or die("Error Connect to DB");
$DB = mssql_select_db("Database");
$SQL = "INSERT INTO Images ";
$SQL .="(Img_Name,Img_Size,Img_Data,Img_Type) 
VALUES ( '$Name','$Size',$HexData,'$Type' )";
$Query = mssql_query($SQL) or die ("error ");		

echo "Upload Complete<br>";
echo "<a href='Img_show.php'>View_img </a> ";
}
?>









Code (img_show.php)
<?
$Connect = mssql_connect("yourhost","user","pass") or die("Error Connect to DB");
$DB = mssql_select_db("Database");
$SQL = "SELECT * FROM Images";
$Query = mssql_query($SQL) or die ("Error Query [".$SQL."]");
?>
<table width="550" border="1" cellpadding="0" cellspacing="0">
  <tr> 
    <th width="50"> <div align="center">Img_Id</div></th>
    <th width="150"> <div align="center">Image</div></th>
    <th width="150"> <div align="center">Img_Name</div></th>
    <th width="150">Img_Size</th>
    <th width="150">Img_Type</th>
  </tr>
  <?  while($objResult = mssql_fetch_array($Query))  { ?>
  <tr> 
    <td><div align="center"><?php echo $objResult["Img_Id"];?></div></td>
    <td><center><img src="img_view.php?Img_Id=<?php echo $objResult["Img_Id"];?>"></center></td>
    <td><center><?php echo $objResult["Img_Name"];?></center></td>
    <td><div align="center"><?php echo $objResult["Img_Size"];?></div></td>
    <td><div align="center"> <?php echo $objResult["Img_Type"];?></div></td>
  </tr>
  <? } ?>
</table>
<?
mssql_close($Connect);
?>





Code (img_view.php)
<? 
 	
$Connect = mssql_connect("yourhost","user","pass") or die("Error Connect to DB");
$DB = mssql_select_db("Database");
$SQL = "SELECT * FROM  Images  WHERE Img_Id = '$Img_Id' ";
$Query = mssql_query($SQL) or die ("Error Query [".$SQL."]");
$Result = mssql_fetch_array($Query);
$Type=$Result["Img_Type"];
header("Content-type: $type "); 
echo $Result["Img_Data"];
?>



Screenshot

PHP SQL Server BLOB








กระทู้อื่น ๆ ที่เกี่ยวข้อง
Go to : PHP Upload File BLOB/Image to SQL Server Database (mssql-OPENROWSET)
Go to : PHP Upload BLOB into Access Database (Access BLOB Binary)
Go to : ใครเคย upload รูปภาพ ลง database mssql (SQL Server Images BLOB) แล้ว ดึงมาแสดง บ้างครับ
Go to : PHP สร้างฟอร์มสำหรับ Upload รูปภาพลงในฐานข้อมูล MySQL พร้อมกับแบบ Form สำหรับการแก้ไขรูปภาพ
Go to : PHP MySQL Multiple Upload File to MySQL Database - CreateElement Input File Upload
Go to : PHP MySQL Upload File to MySQL Database - Multiple Dynamic CreateElement Input File Upload


   
Share
Bookmark.   

  By : tong
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2011-05-12
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่