Image display script won't work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Image display script won't work

Post by drayarms »

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

Code: Select all

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

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.
 


?>  









So any insights as to why the script fails to display the image? Any help is appreciated.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Image display script won't work

Post by Benjamin »

The image more than likely isn't being displayed due to the fact that you have error reporting enabled and are displaying the errors. If you download the image and view it in an editor you might see the errors.

There are a few issues with your code. First you are including the config file twice, which is probably causing some issues. Second, you are accessing array keys without using quotes. That is certainly causing notices or warnings. Third, you aren't escaping the $_SESSION['id'] variable. It's good practice to escape ALL variables being placed in queries.

I see you're sending an image/jpeg header, but what if the image is a png or gif? There's a few more headers you should be sending also, so you may want to look at an example that someone else has already made.

The most important problem here though is that you are storing the actual images in the database. The purpose of a database is to organize data, not store files. The image files should be stored on the file system. The path to the images should be stored in the database. I would recommend refactoring your code to accomplish this.
Post Reply