1

I am trying to insert and retrieve image from database. All is going well and image is being inserted in database in BLOB format, but when I am trying to retrieve those images it does not appear and only broken icon comes to webpage. The code is given below. Please help.

ImageUpload.php

<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
    mysql_connect("localhost", "root", "");
    mysql_select_db ("connection");
    $imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
    $imageProperties = getimageSize($_FILES['userImage']['tmp_name']);

    $sql = "INSERT INTO output_images(imageType ,imageData)
    VALUES('{$imageProperties['mime']}', '{$imgData}')";
    $current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
    if(isset($current_id)) {
        header("Location: listImages.php");
    }
}
}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>

ListImages.php

<?php
    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("connection");
    $sql = "SELECT imageId FROM output_images ORDER BY imageId DESC"; 
    $result = mysql_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
    while($row = mysql_fetch_array($result)) {
    ?>
        <img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>

<?php       
    }
    mysql_close($conn);
?>
</BODY>
</HTML>

imageView.php

<?php
    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("connection") or die(mysql_error());
    if(isset($_GET['image_id'])) {
        $sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
        $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
        $row = mysql_fetch_array($result);
        header("Content-type: " . $row["imageType"]);
        echo $row["imageData"];
    }
    mysql_close($conn);
?>
Nipun
  • 990
  • 1
  • 16
  • 25
Akash
  • 123
  • 2
  • 5
  • 13
  • sorry i could not get your point, please suggest me a option to resolve and show Database Images – Akash Feb 09 '16 at 09:37
  • Sorry, I think I misread your question. What does `imageView.php` generate? Because `Error: Problem on Retrieving Image BLOB
    ` could clearly explain the broken image icon.
    – Álvaro González Feb 09 '16 at 09:41
  • imageView.php is being called from listImage.php and it get image data and type from MySQL BLOB Set the content-type as image (image/jpg, image/gif, …) using PHP header(). print image content. – Akash Feb 09 '16 at 09:47
  • Yes, I understand what your code tries to do, I only read too quickly. What I'm trying to explain is that if `imageView.php` does not generate a valid picture it'd be useful to know what it's generating instead of that. – Álvaro González Feb 09 '16 at 09:51
  • should i print result var in imageView or what to do for check, as if condition is satisfied well – Akash Feb 09 '16 at 10:26

0 Answers0