DontVoteMeDown
DontVoteMeDown

Reputation: 21465

PHP and SQL Server - Retrieve image from an image field

So I have a webcam widget to take snapshots from guests. My guest table have an image field and I want to store this pic on it. I tried storing raw binary data, base64 encoded data and it didn't works. I don't know if database is receiving correct data or if I don't dealing with returned data well. I'm using PDO in my app and I've read on the web that it can have an issue with data larger than 64 kb, anyone knows if it's true?

This is how I got the image data from the webcam:

$imagem = file_get_contents('php://input');

Like I said above I tried to store image's pure data $imagem and then this way:

$data = @unpack("H*hex", $imagem);
$imagem_bd='0x'.$data['hex'];

And for both of that I've tried encoding with base64 too, but none worked.

Just for information: This app I'm working is a remake of another similar app that have this feature too, but on this old app I's used TSQL and it works fine.

Thanks in adv.

EDIT

For now I have done the insert well, but I can't retrieve and render the image at the browser.

Upvotes: 1

Views: 1517

Answers (2)

Marco Biscaro
Marco Biscaro

Reputation: 1268

If you use PreparedStatement, all you need to do is use bindValue, with your binary image:

$sql = "INSERT INTO your_table (image) VALUES (?)";
$stmt->bindValue(1, $imagem);
$stmt->execute();

Anyway, as said here, it's not a good idea to store images in the database.

About the 64KB limit, it depends of the data types of your columns in the table (in MySQL, the BLOB type has a limit of 64KB, but there is another types of data, like MEDIUMBLOB and LONGBLOB).

Upvotes: 1

Venkat Selvan
Venkat Selvan

Reputation: 3126

you can encode images in blob instead of base64. But i recommend you to store image as file and store the link to the image in database. As storing images in database may consume large space and also returning it can eat your server and also it may take more time to load compared to file system.

Upvotes: 3

Related Questions