Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting blob image to file in php

Tags:

php

I'm currently using a simple $_FILES upload script to upload pictures from my iPhone app to server. The image sizes, however, are large and I'd like to resize them before sending the image to the server.

The downside of this, however, is that the resize function converts them to "blob" images (which, as I understand it, is a way of storing the image in the database). I'd prefer to save the files directly to the filesystem. How would I go about converting a blob back into a $_FILE or finding a script that saves blob images to disc??

Thank you!

like image 723
Walker Avatar asked Apr 21 '11 14:04

Walker


People also ask

How do I display a blob file in HTML?

How do I create a blob in HTML? // create Blob from a typed array and strings let hello = new Uint8Array([72, 101, 108, 108, 111]); // "Hello" in binary form let blob = new Blob([hello, ' ', 'world'], {type: 'text/plain'}); let link = document. let link = document.


1 Answers

The BLOB is the binary image. You can write that image to your filesystem once it's on your server. So if your image is in the variable $my_blob, you would do something like

file_put_contents('/path/to/new/file_name', $my_blob);

and there you go.

You might want to save the file to a tmp location first, then do some checks on it before you move it to a final location (with PHPs rename() function).

Btw: why not just save the BLOB to DB? That is a legitimate way of handling files these days, that's what the BLOB MySQL data type is for after all.

like image 53
Christof Avatar answered Sep 21 '22 06:09

Christof