How to read image field from MSSQL with PHP
I am creating a web site that needs to synchronise an online MySQL database reading from the offline MSSQL server. All communication and reading all fields from MSSQL is working OK except that image field.开发者_运维知识库 I have been working with PHP and Mysql for some time and know how to insert/retrieve images to/from a MySQL database(using BLOBS). I have tried the same concept first with MSSQL and I just can't get it to work. Does anyone have any experience working with inserting/retrieving images from MSSQL using php? It has been busting my balls for a few days already.. Here are the code combinations that I have tried and didn't work..
<
?php
$conn = mssql_connect("COMP1");
mssql_select_db("datalab",$conn);
// storing a file
$datastring = file_get_contents("banner.jpg");
$data = unpack("H*hex", $datastring);
mssql_query("insert into milos (id,naziv, slika)
values ('2','img2.jpg', 0x".$data['hex'].")");
// retrieving
$result = mssql_query("select slika from milos where naziv = 'img2.jpg'");
$row = mssql_fetch_array($result);
header("Content-type: image/jpeg;");
$data = $row['slika'];
echo $data;
//echo $slika;
?>
<
?php
$conn = mssql_connect("COMP1");
mssql_select_db("datalab",$conn);
$result = mssql_query("select slika from milos where naziv = 'img.jpg'");
$row = mssql_fetch_array($result);
header("Content-type: image/jpeg;");
$data = $row['slika'];
$datas = substr($data,78);
echo $datas;
?>
<
?php
$conn = mssql_connect("COMP1");
mssql_select_db("datalab",$conn);
$result = mssql_query("select slika from milos where naziv = 'img.jpg'");
$row = mssql_fetch_array($result);
header("Content-type: image/bmp;");
$data = $row['slika'];
$data_pos = strpos($data,"JFIF");
$datas = substr($data,$data_pos - 6);
echo $datas;
?>
Can any one help me?
Thanks in advance.. Nimmy....
Search php.ini and find the lines
;mssql.textlimit = 4096
and
;mssql.textsize = 4096
Replace the value to 2147483647
like this:
mssql.textlimit = 2147483647
mssql.textsize = 2147483647
Also you can do the following just after making a connection.
mssql_query("SET TEXTSIZE 2147483647");
You need to upload SQL Server 2005 Driver for PHP. Please find more information here.
精彩评论