2

I've been pulling my hair out for a couple of days about the issue i'm having.

Trying to read a PDF that is stored as binary blob data in a SQL database (not mysql) varbinary field.

If I use a program like Navicat, and save the raw blob data directly as a PDF, the file works.

When I try and read the data through PHP from a mssql query and create a PDF file -

$file = "file.pdf";
$data = $row['FileData'];
file_put_contents($file, $data);

It creates the PDF, however it is corrupt and will not open.

I have difficulty in knowing exactly what format the data is stored as in the database. When I view the BLOB data in Navicat, it renders it like this:

%PDF-1.4
%Çì¢
5 0 obj
<</Length 6 0 R/Filter /FlateDecode>>
stream
xœÅY[o·F_÷Wð%èlðÞûX¸-8èCÐy-Éjt±´qÚüŒö÷#‡Crv¹Y§ÞÔ`qÈÃÃsùÎ…Ô“#    ±ñglïW_çÄÍn•¦ÅwɃç›ÕÓÊ*þKíx{/þxAR‹‹ë•CZN\I8ŠßF8FcI\ܯ†ß­/þ±b3jlx?â?    ¶rÔÊ‚áÅÛ¼}3Ïm2£y¡·ÿO«WÐB
'ÉŽÖ‰û•Æ—Ï»é[¢¸$”•ViGÅ^<_­®¿\Ù‘øçÊ‹oAûÍj£a\’¡ÙBÉÞ¿´q×ë$TÆqRúI<–3weÆúežwÕ
9q–a›¢W¯~¢«ÕHŒ^£l\Ìÿê7–‹H^–

It's hard to show you what the raw data in the database looks like, as binary data as I believe can't be viewed properly, and what you see is just the interpretation of what it really is.

Any help would be appreciated. Thank you.

9
  • You may need external libraries to fully manipulate pdf from BLOB: stackoverflow.com/questions/467793/… Commented Mar 10, 2015 at 3:07
  • what is the php that retrieves the data from the table? your db method might be screwing up the data by trying to sanitize it or something Commented Mar 10, 2015 at 3:13
  • @VladimirRamik you shouldnt need an external library to pull binary data from a database and write it to a file. OP is not manipulating pdf data, they are just trying to pull it and write it to disk Commented Mar 10, 2015 at 3:15
  • I would avoid using any and all external libraries if possible. Was simply sharing a good link I've used before. Commented Mar 10, 2015 at 3:17
  • 1
    Thanks @chiliNUT - it's super frustrating as it normally would just work. I'm not doing anything complicated, and I know the data is binary stored in the database. I appreciate your input. Commented Mar 10, 2015 at 3:49

2 Answers 2

1

Solved the issue by adding the following to my .htaccess file

php_value mssql.textlimit 10000000
php_value mssql.textsize 10000000

Basically it was writing the files, but it was stopping 63kb (64,512 bytes) into the file, so increasing this limit enabled the script to finish writing the remainder of the file.

Hope this helps someone else with the same issue.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried this?

select hex(pdf) from table;

Use the above query and use file_put_contents(); to store your contents onto a file.

PHP does a great job providing easy conversions between binary to hex and hex to binary.

1 Comment

I did, however I receive the error: [Err] 42000 - [SQL Server]'HEX' is not a recognized built-in function name. I am using SQL server here, not mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.