Jump to content

image stored in database can't be displayed.


drayarms

Recommended Posts

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

 

$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( 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:

<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.

 

<?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:

<?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.

 

 

 

 

 

 

Link to comment
Share on other sites

Because of the multiple problems in the picscript.php code, I recommend temporarily commenting out the header() statement and browsing directly to the picscript.php file. This will show you the actual output, including any php detected errors.

 

You also need to use E_ALL for your error reporting setting (by using  & ~E_NOTICE you are hiding notice messages, which can also hide problems in your code that is preventing it from working.)

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.