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

Recommended Answers

Answered by diafol 3,720 in a post from

Can you elaborate on the business logic? Can't you just save the blob data to a variable?

Jump to Post

All 4 Replies

Member Avatar

Can you elaborate on the business logic? Can't you just save the blob data to a variable?

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.

@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.

Member Avatar

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;?>">
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.