Jump to content

Image display script won't work


drayarms

Recommended Posts

I came up with the following script for displaying an uploaded picture on a webpage but for some reason I can't pinpoint, the picture won't be displayed.  I checked my database from php myadmin and sure enough, the picture was successfully uploaded but somehow, the picture won't be displayed.  So here is the display script.  I named it display_pic.php

 


<?php


//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');

                             //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 image_cartegory = 'main' ");
                             $row = mysql_fetch_assoc($rs);
                             $imagebytes = $row[image];
                             header("Content-type: image/jpeg");
                             print $imagebytes;

                            
?>

 

 

The tag on the html page that's supposed to display the picture reads something like this

<img src="display_pic.php"  width="140" height="140">

 

 

 

And just in case this might help, I will include the image upload script below, which I think worked just fine because my values were successfully inserted into the database.

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



?>  



So any insights as to why the script fails to display the image? Any help is appreciated.

 

Link to comment
Share on other sites

nevermind i see that its just the directory, what i like to do is save the file into a directory, and save the path to that directory in my db...then use the image path in my db as the src of an <img> tag

Link to comment
Share on other sites

$imagebytes = $row[image];  should be

$imagebytes = $row['image'];

 

 

@fugix, the upload script appears to be saving the image as a massive string in the database, never seen that before, personally i do what you suggested. only i keep the path in my 'common' include, and save image name in db

Link to comment
Share on other sites

@wildteen98,  no error message appears.  the image div where the image is supposed to appear just displays a tiny jpeg icon right at the center

 

@fugix, i'm saving directly into the database. i first want to see if i can successfully save and retrieve directly from the database b4 trying the directory method.  do u have any script that demonstrates how to save the file in a directory and save the path in the database?

 

@spiderwell, i tried ur suggestion

$imagebytes = $row['image'];

no luck with that.

 

please more suggestions.

 

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.