I am saving user's images as blob in database. problem is that after retrieving that blob image,i want to save it in a temporary location of web server and from there i want to apply some business logic. please anybody help me to solve this. Thanks in advance

4 Years
Discussion Span
Last Post by diafol

Same thing here, want to see more elaboration on bus. logic. It all depends on how your script stored the binary info. of the image. Is it base64 encoded or just a plain binary? I am not sure, but I think most BLOB are base64 encoded.

Let us assume that is indeed a base64 encoded, then we can convert it by using fwrite(), or file_put_contents. I am not 100% sure with the file_put_contents though. I need to look it up.

Anyways, if my assumptions above are correct, then blob can be saved as a physical image by example codes below. This is not a tested approach, please consider this as an example and by no means an ultimate solution to your question.

   ## assuming that image is the column containing the BLOB
   $thisBlob = $row['image'];
   ## and the image extension is also stored in ext column
   $ext = $row['ext'];

   ## use fwrite() function
   $physicalFile = fopen('temporarylocation/newImage'. $ext ,"w");
   ## write the physical file
   fwrite($physicalfile, base64_decode($row['image']));


The file_put_contents method, I still have to experiment with it, because I have never done it before. I used it with simple xml and html files but not with BLOB.


I just found out that some are not base64 encoded. If that is your case remove the base64_decode in the frwire() function.

Edited by veedeoo: info added


@diafol, actually after saving that image file to a temporary location i want to assign that location as src to an image.when user sign out i want to delete it from that temporary location. and one more thing i am able to fetch that image file details i.e. file content,format etc. from database.just i need the solution to save it in temporary location.


If you have the blob info in the DB, you don't need to store it as a physical file. You can create an image file from php or you can use:

$type = "image/png"; //or the actual mime type of the file
$base64blob = base64_encode($blobfromDB); //encode to base64
$datauri = "data:$type;base64,$base64blob";
<img src="<?php echo $datauri;?>">
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.