Home > Software design >  how to retrieve BLOB-23B.bin back as image
how to retrieve BLOB-23B.bin back as image

Time:10-05

enter image description here

I am creating program that stored file uploaded as long blob type. how to retrieve it back as common file?

Data was stored like this

$sql = "INSERT INTO leaves(companyname,responsiblename,description,photos)
        VALUES('$companyname','$responsiblename','$description','$photos')";
if (isset($_POST['Apply'])){ 
    $companyname = $_POST['companyname']; 
    $responsiblename = $_POST['responsiblename']; 
    $description = $_POST['editor1']; 
    $photos = $_POST['fileToUpload']

CodePudding user response:

As "common file" I assume you need to write it to a file.

Let's start after the point where you retrieve the blob from the db.

At first, you need to determine the mime type if you didn't save the mime type in the database row.

Then you send the data to the browser

$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$result = mysqli_query("select photos from leaves");
$row = mysqli_fetch_assoc($result);
$photo=$row['photos'];

$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimetype = $finfo->buffer($photo);
header("content-type: " . $mimetype);
header("Content-Length: " . strlen($photo));
echo $photo;

VERY VERY IMPORTANT!!!

Do not send output to the browser (ie echo some data) other than echo $photo.

If you send output before the header() function you get the following error: Warning: Cannot modify header information - headers already sent by (output started at....

If you send output after the header() function your image will be corrupted

  • Related