Hi all,
I'm discovering ms sql server with php development, I'm at the beginner level !
I saw a lot of sites, forums, questions but I haven't found my answer or it doesn't work ! Maybe I do wrong or ignore a big point.
I'm trying to retrieve a pdf file stored in a binary field (configured as varbinary(max)) from my database without any orm layer or other mess ! Only freetds and mssql_* functions.
I got a connection, trigger a query and retrieve the results, no problem.
And then, impossible to restore my data in a good way to set the content of my http'response.
Is it possible or not to deal like this to display a pdf file stored in database ? Or am I compelled to get it by a procedure and let sql server do the job ?
To me, we should succeed by the php way.. (And it seems a lot of people succeed in it, except me !)
Some of my code to get this behavior :
// connection and retrieve
$sql_select = sprintf('select docstring from docstring');
$connexion = mssql_connect('server', 'username', 'password');
if (!$connexion || !mssql_select_db('database', $connexion))
{
die('Not connected');
}
$version = mssql_query($sql_select);
$row = mssql_fetch_array($version);
// [ $row is an 1-element array containing my binary data at $row[0] ]
// some try to display it in my browser
// 1.
header ('Content-type: application/pdf');
print ($row[0]);
// 2.
header ('Content-type: application/pdf');
$unpacked = unpack('H*hex', $row[0]);
print ($packed['hex']);
// 3.
header ('Content-type: application/pdf');
echo mssql_result($version, 0, 0);4.
build a file and send it to the browser : doesn't work too and I found it a little dirty cause if we can build a pdf file from these data, we can display it on a brower, in my mind...
I always get a display error in browser or in a pdf reader saying the file must be corrupted or wrong decoded...
About my environment :
sql server : 10.50.1600 : SQL Server 2008 R2 RTM
php 5.3
freetds tds version : 4.2 (also tested with 7.0)
example of what I see in my binary data from mssql management studio: 0x255044462D312E350D25E2E3CFD30D0A3136312030206F626A0D3C3C2F4C696E656172697A656420312F4C2[...]
Have you ever met this kind of issue ? I've seen a lot of posts but never resolve mine.. And i'm a little desperate !
Thanks for reading !
Big thanks for answering !