1

I've managed to put the code to download a medium blob saved in mysql table. The code works in terms of it retrieves the right file, names it correctly, gives it the correct extension and the browser force downloads it.

The thing is when I try to open file I get an error. That is for example if it is a jpeg file the image won't show. If it is a pdf I do not get the pdf. I checked the file size and it seems to correspond to the stored Blob file size.

I think it is a format issue but do not know how to correct it. Any help appreciated. My code is:

Front End:
<a href="data/docmgmt/get-docmgmt.php?id=<?php=$id;?>">Test</a>

BackEnd:
$doc_id = 24122565639274498;
$conn = new PDO("mysql:host=localhost;dbname=$dbname", $db->id, $db->pass); //connect to db
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    //error modes                      
$stmt = $conn->prepare('SELECT content,type,name,size FROM docmgmt WHERE doc_id=:doc_id');
$stmt->bindParam(':doc_id', $doc_id, PDO::PARAM_INT);
$stmt->execute();               
$stmt->bindColumn(1, $lob, PDO::PARAM_LOB);
$stmt->bindColumn(2, $type, PDO::PARAM_STR, 256);
$stmt->bindColumn(3, $name, PDO::PARAM_STR, 256);
$stmt->bindColumn(4, $size, PDO::PARAM_INT);
$stmt->fetch(PDO::FETCH_BOUND);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $lob;

Screen Dump of contents of PNG file retrieved

enter image description here

Ka Tech
  • 8,937
  • 14
  • 53
  • 78
  • I think this answer might be helpful to you http://stackoverflow.com/questions/20556773/php-display-image-blob-from-mysql – dops Jul 25 '15 at 10:52
  • Thanks I tried echo base64_encode($lob); on the last line and added exit; still not luck. – Ka Tech Jul 25 '15 at 10:58
  • No, that's not what I meant, I meant you should base64 encode the file before its written to the database. Ie: `$file = base64_encode($_FILES['input_name'][tmp_name]);` (not so literally though, obviously you should still do some sanitization). Then you'll be able to `echo base64_decode($lob);` – andrew Jul 25 '15 at 11:54

2 Answers2

0

It could be some kind of encoding issue I'd try base64 encode / decode on the data on the way in / out of the database

Also make sure you have no whitespace in the source as that would corrupt the output.

Check:

<-- Here
<?php

and

?>
<-- here

or better still remove the final closing php tag altogether

andrew
  • 9,313
  • 7
  • 30
  • 61
0

Maybe my simple encoding function can help you. As @andrew said it's most likely not working because you're not using base64 encoding / decoding

Send a file like

<input type="file" name="image" accept="image/*">


Process it like

// Make sure the user uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

    // Temporary file name stored on the server
    $tmpName = $_FILES['image']['tmp_name'];

    // Read the file
    $fp = fopen($tmpName, 'r');
    $data = fread($fp, filesize($tmpName));
    $data = addslashes($data);
    fclose($fp);
}


Store the $data in db then read it with

$img=base64_encode($row['image_from_db']);


put the $img into the display function

// Return <img> if there is any provided.
function displayImage(&$link){
   if(!empty($link)){
          return "<img alt=\"Profile Picture\" src=\"data:image/*;charset=utf8;base64, $link \">";
       }
       else
       {
          return "<img alt=\"Profile Picture\" src='../img/nopicture.jpg'>";
       }
 }


If you want other files than images you need to change the input accept="image/*" and the a display function that fits your needs.

shadryck
  • 191
  • 4
  • 17