Jump to content

Retrieving image from database (BLOB)? Just getting binary.


fredted40x

Recommended Posts

Hi,

 

I have managed to get the code working to store a .jpg file in the database under the longblob type.

 

Now all i have left to do is to retrieve that image and display it.

 

So far i have this:

 

list.php

while($r = mysql_fetch_array($sql)) { //for each record
...
echo "     <img src=  getoutside.php?id='".$r[apartmentId].  " '>     ";

 

 

getoutside.php

<?php
header("Content-type: image/jpg"); // act as a jpg file to browser
    $nId = $_GET['id'];
include 'dbase.php'; //connect to database
    $sqlo = "SELECT outside FROM apartment WHERE apartmentId = $nId";

$oResult = mysql_query($sqlo);

$oRow = mysql_fetch_array($oResult);
$sJpg = $oRow["outside"];
echo $sJpg;
?>

 

The result from this is a box with a red cross in it.

Can anyone find a problem with this code please?

 

Thanks

Link to comment
Share on other sites

can you explain please? Dont think i've used them before.

 

Heres the code to add the image if it helps.

 

//get file information --step1
  $fileName = $_FILES['userfile']['name'];
  $tmpName = $_FILES['userfile']['tmp_name'];
  $fileSize = $_FILES['userfile']['size'];
  
  //get file content -- step1
  $fp = fopen($tmpName, 'r');
  $content = fread($fp, $fileSize);
  $content = addslashes($content);
  fclose($fp);

Link to comment
Share on other sites

You have used double quotes inside double quotes in that line that i mentioned in the previous reply. These sort of statements confuse the engine on where to start and where to end.

In other words when you echo anything and put first double quote then all the compiler(or whatever it is called :)) has to search another double quote and prints out everything that is in between them.

 

You should add \ before every double quote that is in between to let the compiler know that its just a literal.

Link to comment
Share on other sites

Are you able to check the insert code for me please. Im thinking it may either be that or the getoutside.php file.

 

<form action="admin.php" method="post" enctype="multipart/form-data" >
...
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
...

 

 

admin.php

 

include 'dbase.php'; //connect to database
  
  
  //get file information --step1
  $fileName = $_FILES['userfile']['name'];
  $tmpName = $_FILES['userfile']['tmp_name'];
  $fileSize = $_FILES['userfile']['size'];
  
  //get file content -- step1
  $fp = fopen($tmpName, 'r');
  $content = fread($fp, $fileSize);
  $content = addslashes($content);
  fclose($fp);

$query = "INSERT INTO apartment VALUES(NULL,...,'$content')";
  mysql_query($query)

Link to comment
Share on other sites

Thanks again for replying.

 

That line seems to work but without errors now but im still getting a white box with a cross in it.

 

Its says the location for the image is

http://localhost/webs/getoutside.php?id='".15.

 

and if i remove the two ' it says the location is

http://localhost/webs/getoutside.php?id=".15.

 

Do you need the full stops? If i remove them they go from the url as well.

 

It doesnt seem to be calling getoutside.php, from what i can gather it thinks that it is a image.

 

 

 

 

edit:

If i change src to href i get a white box with a different icon in it

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.