Page 1 of 1

image stored in database can't be displayed

Posted: Sat Feb 19, 2011 4:57 am
by drayarms
I put together the following blocs of code for uploading pictures into a database and displaying them on a webpage. The pictures are supposed to be displayed on the member's only page of a website I'm working on, upon logging in, and they are supposed to be the member's uploaded picture. I created several members and and used one of my existing member accounts to test the uploading process. The picture upload process appeared to have been successful when I checked on myphpadmin. Yet, when I login with this account, no picture is displayed, instead, a tiny jpg icon is displayed at the top left corner of the box in which the picture was supposed to be displayed. Same thing when I login with the other accounts with which I haven't yet uploaded a picture.

I'll start with the code that installs the table in the database

Code: Select all

$query = "CREATE TABLE images (

image_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
member_id INT UNSIGNED,
like_id INT UNSIGNED,
image LONGBLOB NOT NULL,
image_name varchar(255)  NOT NULL,
image_type varchar(4) NOT NULL,
image_size int(8) NOT NULL,
image_cartegory VARCHAR(20) NOT NULL,
image_path VARCHAR(300),
image_date DATE  

          )";

Then here is the code which allows the member to upload his picture:

Code: Select all

<form enctype="multipart/form-data" action="insert_image.php" method="post" name="changer">
                         <input name="MAX_FILE_SIZE" value="102400" type="hidden">
                         <input name="image" accept="image/jpeg" type="file">
                         <input value="Submit" type="submit">
                         </form>

And here is the insertimage.php which inserts the image into our database: Note that I have to authenticate the user in order to register his session id which is used later on in the select query to identify him and select the right image that corresponds to him.

Code: Select all

<?php

//This file inserts the main image into the images table.

//address error handling

ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);





//authenticate user
//Start session
	session_start();

       
        //Connect to database
require ('config.php');
	
	//Check whether the session variable id is present or not. If not, deny access.
	if(!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
		header("location: access_denied.php");
		exit();
	}

        else{ 
// Make sure the user actually 
// selected and 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);
      

      // Create the query and insert
      // into our database.
      $query = "INSERT INTO images (member_id, image_cartegory, image_date, image) VALUES ('{$_SESSION['id']}', 'main', NOW(), '$data')";
      $results = mysql_query($query);
      
      // Print results
      print "Thank you, your file has been uploaded.";
      
}
else {
   print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close();


} //End of if statmemnt.
 


?>  

On the page which is supposed to display the image upon login in, I inserted the following html code in the div that's supposed to contain the image:


<div id="image_box" style="float:left; background-color: #c0c0c0; height:150px; width:140px; border- color:#a0a0a0;border-style:outset;border-width:1px; margin:auto; ">

<img src=picscript.php?imname=potwoods>

</div>


And finally, the picscript.php contained the select query:

Code: Select all

<?php

                             //address error handling

                             ini_set ('display_errors', 1);
                             error_reporting (E_ALL & ~E_NOTICE);

                             //include the config file
                              require('config.php');

                             $image = stripslashes($_REQUEST[imname]);
                             $rs = mysql_query("SELECT* FROM images WHERE member_id = '".$_SESSION['id']."' AND cartegoty = 'main' ");
                             $row = mysql_fetch_assoc($rs);
                             $imagebytes = $row[image];
                             header("Content-type: image/jpeg");
                             print $imagebytes;

                            
 ?>



Now the million dollar question is, "What is preventing the picture from getting displayed?" I know this is a very lengthy and laborious problem to follow but I'm sure there is someone out there who can point out where I'm not getting it. Thanks.

Re: image stored in database can't be displayed

Posted: Sat Feb 19, 2011 9:50 am
by rhecker
Firstly, its almost always a terrible idea to store images in the database. It gives MySQL indigestion. I learned the hard way to never do that. But if you must have it that way, here is the code from an old application that displays the image. The page that displays the images must call a different page which contains the code to construct the image, as follows.

The display code is like this:
<img src = "getimage.php?id=6">

Then getimage.php is something like this:

call the table, then:

Code: Select all

$id = $_GET['id'];
  $sql = "SELECT photodata
      FROM teachers WHERE id = '$id'";
  $result = @mysql_query($sql);
  if (!$result) {
    exit('Database error: ' . mysql_error());
  }  
  $file = mysql_fetch_array($result);
  if (!$file) {
    exit('File with given ID not found in database!');
  }
  $photodata = $file['photodata'];
  $disposition = 'inline'; 
  header("content-disposition: inline");
  header("content-type: image/pjpeg");
  header('content-length: ' . strlen($photodata));
  echo($photodata);
  exit();

Re: image stored in database can't be displayed

Posted: Sat Feb 19, 2011 7:43 pm
by s.dot
The error reporting on your image display script is what's preventing it (or at least one problem that's keeping it from displaying). Visit picscript.php in your browser and you will see the problem.