1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to Convert an Image back from a Blob in a mysql ?? HELP

Discussion in 'PHP' started by xkaser101, Aug 2, 2011.

  1. #1
    Hello,
    I have been trying for a long time to get this to work I searched everywhere and they all end up with the same answer which does Not work for me how ever I might be doing something wrong please take a look at my code and tell me how to fix it,

    What I am trying to achieve is pretty simple, I save an image to a database using Blob Type, All works fine but i am having a trouble Displaying the image back.

    Here is the code from the get_image.php :
    
    <?php 
    include('includes/mysql_connect.php');
    
    $id = (int) $_GET['id'];
    $q = "SELECT `image` FROM `testi` WHERE id='$id'";
    		$r = mysqli_query($db_con_misc, $q);
    		$image = mysqli_fetch_assoc($r);
    		header("Content-type : image/JPEG ");
    echo $image['image'];
    ?>
    
    Code (markup):
    Here is the code from edit_testimonials.php :

    
    if(isset($_POST['submit'])){ 
    	$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    	$q = "INSERT INTO `testi` (`message`,`image`,`active`,`by`) VALUES ( '{$_POST['message']}', '$image', '{$_POST['active']}', '{$_POST['nickname']}')";
    	$r = mysqli_query($db_con_misc, $q);
    	if($r){
    		$last_id = mysqli_insert_id($db_con_misc);
    		echo "<img src=\"get_image.php?id=1\" /><p />";
    		echo "Testimonial has been added <a href=\"home.php\">- Go back</a><p />";	
    		exit();
    	}else{
    	die("Database Error : " . mysqli_error($db_con_misc));	
    	}
    }
    ?>
    <body> // html form bellow
    <fieldset><legend>ADD Testimonial</legend>
    <form action="edit_testimonials.php" method="post" enctype="multipart/form-data">
    Message :- <textarea name="message"></textarea><br /><br />
    Nickname :- <input type="text" name="nickname" value=""  /><br /><br />
    Image :- <input type="file" name="image" value=""  /> (64 KiB Max) <br /><br />
    Status :- <input type="text" name="active" value="" /> ( yes or no )<br /><br />
    <input type="submit" name="submit" value="ADD" /><br /><br />
    </form>
    </fieldset>
    Code (markup):
    It is pretty simlple actually I add an image to the database and then it displays it by sending the Id of the image to another php page (get_image.php) and it returns with the image... Please help me all i get is a broken image and when i try to do this directly on the get_image.php?id=XXX it returns with the Binary Rubbish That was originally inserted into MySQL.

    Thanks in advance,
    Kind Regards,
    Issa Shamoun.
     
    xkaser101, Aug 2, 2011 IP
  2. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #2
    change this:
    header("Content-type : image/JPEG ");

    to this:
    header('Content-type: image/jpg');
     
    The Webby, Aug 3, 2011 IP
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165