note 57379 deleted from function.mssql-query by mazzanet
| From: | mazzanet@php.net | Date: | Sun, 02 Oct 2005 13:52:42 +0000 |
| Subject: | note 57379 deleted from function.mssql-query by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96187@lists.php.net to get a copy of this message | ||
Note Submitter: abelzelda at yahoo dot es
----
Here it's a very easy way to insert image into a
SQL SERVER using PHP
-First:
Create formulary.php:
//---------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>Binary to DB</TITLE>
</HEAD>
<BODY>
<FORM enctype="multipart/form-data" method="post"
action="../Pages/insert.php">
Archivo: <INPUT type="file" name="archivo" size="30">
<INPUT type="submit" name="submit" value="Subir archivo">
</FORM>
</BODY>
</HTML>
//---------------------------------------------------------------
-Second:
Create insert.php
//--------------------------------------------------------------
<?php
// Check if it was loaded an is correct.
if (empty($_FILES['file']['name'])){
header("location:../Pages/formulary.php?process=
mising_indicated_file"); //whenever formulary is called ..
exit;
}
//Entablish conexion with data base.
require("../adodb/adodb.inc.php");// using ADO DB Data POWER
$conexion =& ADONewConnection('odbc_mssql');
$dsn = "Driver={SQL Server};Server=Server;Database=DB;";
$conexion->Connect($dsn,'user','password');
// Temp file (ruta y nombre).
$binary_name_temp=$_FILES['file']['tmp_name'] ;
//The MSSQL documentation states that binary constants should be represented by their unquoted
hexadecimal byte-string. That is.. to set the binary column "col" to contain the bytes
0x12, 0x65 and 0x35 you shold do "col = 0x126535" in you query.
$datastring = file_get_contents($binary_name_temp);
$data = unpack("H*hex", $datastring);
$sql = "insert into Table (Image)
values (0x".$data['hex'].")";
$recordSet = $conexion->Execute($sql);
if (!$recordSet)
print $conexion->ErrorMsg();
else
{}
//Close the conexion
$recordSet->Close();
$conexion->Close();
?>
Done !!!!!!!!!!!!!!!!!!
Enjoy :D :D :D :D :D
//From Cuba to the people//