Help Reading Binary Image Data from SQL Server into PHP

Posted by Joe Majewski on Stack Overflow See other posts from Stack Overflow or by Joe Majewski
Published on 2010-04-13T15:12:02Z Indexed on 2010/04/13 18:13 UTC
Read the original article Hit count: 333

Filed under:
|
|
|
|

I cannot seem to figure out a way to read binary data from SQL server into PHP. I am working on a project where I need to be able to store the image directly in the SQL table, not on the file system.

Currently, I have been using a query like this one:

INSERT INTO myTable(Document) SELECT * FROM OPENROWSET(BULK N'C:\image.jpg', SINGLE_BLOB) as BLAH

This works fine to actually insert the image into the table, but I haven't yet figured a way to retrieve it and get my image back.

I am doing this with PHP, and ultimately will have to make a stored procedure out of it, but can anyone enlighten me on a way to get that binary data (varbinary(MAX)) and generate an image on the fly.

I expected it to be simple to use a SELECT statement and add a content-type to the headers that indicated it was an image, but it's simply not working. Instead, the page will just display the name of the file, which I have encountered in the past and understand it to be an error with the image data.

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about sql