Hi,

I have a problem with retrieving uploaded images from a database. It appears to upload fine, but when i try to display the image i get loads of binary jargon?

I have tried looking everywhere for a solution, but unable to, any help would be greatly appreciated.

The code for uploading is

include ("database.php"); 
  
  if(isset($_POST['Submit']))	{	
	if(isset($_FILES['image'])) {		
	
	/* Upload file from anywhere and put in a set temp dir  */  	
	$image = file_get_contents($_FILES['image']['tmp_name']);
	$image = mysql_real_escape_string($image);
		
	$handle = fopen($image, 'rb'); 	// read binary
	$contents = fread ($handle, filesize ($image));	
	//$contents = addslashes($contents);	
	fclose ($handle);	 
		
	$description = $_POST['description'];
		
	/* Save uploaded file into database	*/	
	$query = "INSERT INTO blob_eg.upload VALUES ('', '$image', '$description') ";
	$result = mysql_query($query) or die("Couldn't add file to database");	
				
	// Check 
	// echo "<br /> '$query' <br />";	
	echo "<br /> '$result' <br />";		
	echo "File uploaded <br /><br />" ;
	echo "<a href=\"!upload.html\"> upload more  </a><br /><br />" ;
	echo "<a href=\"!retrieve.php\"> view image  </a><br /><br />" ;				
	//echo "<input type=\"button\" name=\"retrieve\" value=\"Retrieve image\" ><br />" ;		
	  } // isset upfile
	} // isset submit

and the code for displaying is

include ("database.php");		
		
	if(isset($_GET['id']))	{	
		$id = $_GET['id'];	}	
	
	$query  = "SELECT image FROM blob_eg.upload WHERE id='7' ";		
	$result = mysql_query($query) or die(mysql_error());
	
	if (mysql_num_rows($result) == 0) die("There is no image.");

	echo "<br /> '$query' <br />";
	echo "<br /> '$result' <br/ > \n";	
  
    $row       = mysql_fetch_array($result);	 
	$content = $row['image'];
//	$content = mysql_real_escape_string($row[image]); 
//	header("Content-type: image/jpg");
// 	echo $content ;		
	echo "<img src=\"!retrieve.php?id='id'\" width=\"250\" height=\"180\" >";	
	
/*	while ($row = mysql_fetch_array($result)) {	
		extract($row);	
		echo "<img src=!retrieve.php?id='id' width=200 height=150 border=0>";	
		echo $image;		}	 	*/

Tell the browser about the image by setting the header.

header('Content-type: image/jpeg');
And then execute the query and get the result set
$query = "SELECT image from sample_images where id=$someid";
$result = mysql_fetch_array(mysql_query($query));

then decode it by using..
echo base64_decode($result["image"]);

Thank you :)

Now the output is the URL of the script (http://localhost/..../!retrieve.php). Could there also be a problem with uploading the image in the right format?

Here is the display script with the corrections

include ("database.php");		
		
	if(isset($_GET['id']))	{	
		$id = $_GET['id'];	}	

// Tell browser of image
	header('Content-type: image/jpeg');

// Select image
	$query  = "SELECT image FROM blob_eg.upload WHERE id='6' ";		
	$result = mysql_query($query) or die(mysql_error());
	
	if (mysql_num_rows($result) == 0) die("There is no image.");

	echo "<br /> '$query' <br />";
	echo "<br /> '$result' <br/ > \n";	

//  Decode result
	echo base64_decode($result["image"]);  
	
//  Retrieve image
	$row     = mysql_fetch_array($result);	 
	$content = $row['image'];
	$content = mysql_real_escape_string($row[image]); 	

//	Display image
	echo $content;
//	echo "<img src=\"$content\" width=\"250\" height=\"180\" > " ;

decode this one $row; not result;

Try this..

BTW it is preferable to store only Path in the DB and put your images into some folders and when you want to display the images then just get that path and display it.

Thank you so much.

Irony is i can save and retrieve from a folder just fine.

In !upload.php

// Upload file - must be in same dir as !upload2.php  	
		$image = file_get_contents($_FILES['image']['name']);
        $image = mysql_real_escape_string($image);
		
		if (is_uploaded_file($_FILES['image']['tmp_name']))  {		
		       if (!move_uploaded_file($_FILES['image']['tmp_name'], $image))    {	
			       echo 'Problem: Could not move file to destination directory';
				   exit;
			  } // end if
		   } // end if			

 $query = "INSERT INTO blob_eg.upload VALUES ('', '$image', '$description') ";
 $result = mysql_query($query) or die("Couldn't add file to database");

But how do you retrieve the path and save path in the database?

Also (silly question), each image is associated with a particular property (its a real estate system). Can this be done by saving paths?

Property u mean width and height etc?

The script to upload image in some folder is

/*------ html input filed */

<input name="uploadedfile" type="file" />

--------------------------------------------

/*----PHP script----*/

$target_path = "yourTargetFolder"; //give here full path of your target folder where u want to upload your image

$target_path = $target_path . basename( $_FILES);

if(move_uploaded_file($_FILES, $target_path))

{

}
after this script your image will be in targeted folder.

For details information read this

http://www.w3schools.com/php/php_file_upload.asp

Property u mean width and height etc?

The script to upload image in some folder is

/*------ html input filed */

<input name="uploadedfile" type="file" />

--------------------------------------------

/*----PHP script----*/

$target_path = "yourTargetFolder"; //give here full path of your target folder where u want to upload your image

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))

{

}

after this script your image will be in targeted folder.

For details information read this

http://www.w3schools.com/php/php_file_upload.asp

This is an interesting subject, I have played around with trying to store images in the db but I have always had issues with it. If I found a good way to accomplish it, I might use it in my projects but since I haven't I always end up using the other method of just using PHP to physically move the file to a storage folder and store certain pieces of information about the file into the DB.

One question,

One this line, $query = "INSERT INTO blob_eg.upload, what does blob_eg.upload refer to? Is that just a shortcut to be able to refer to the table and table field you want to perform the insert on?

Ex. INSERT INTO table.field VALUE...

Hi,

I have a problem with retrieving uploaded images from a database. It appears to upload fine, but when i try to display the image i get loads of binary jargon?

I have tried looking everywhere for a solution, but unable to, any help would be greatly appreciated.

The code for uploading is

include ("database.php"); 

  if(isset($_POST['Submit']))   {   
    if(isset($_FILES['image'])) {       

    /* Upload file from anywhere and put in a set temp dir  */      
    $image = file_get_contents($_FILES['image']['tmp_name']);
    $image = mysql_real_escape_string($image);

    $handle = fopen($image, 'rb');  // read binary
    $contents = fread ($handle, filesize ($image)); 
    //$contents = addslashes($contents);    
    fclose ($handle);    

    $description = $_POST['description'];

    /* Save uploaded file into database */  
    $query = "INSERT INTO blob_eg.upload VALUES ('', '$image', '$description') ";
    $result = mysql_query($query) or die("Couldn't add file to database");  

    // Check 
    // echo "<br /> '$query' <br />"; 
    echo "<br /> '$result' <br />";       
    echo "File uploaded <br /><br />" ;
    echo "<a href=\"!upload.html\"> upload more  </a><br /><br />" ;
    echo "<a href=\"!retrieve.php\"> view image  </a><br /><br />" ;                
    //echo "<input type=\"button\" name=\"retrieve\" value=\"Retrieve image\" ><br />" ;      
      } // isset upfile
    } // isset submit

and the code for displaying is

    include ("database.php");       

    if(isset($_GET['id']))  {   
        $id = $_GET['id'];  }   

    $query  = "SELECT image FROM blob_eg.upload WHERE id='7' ";     
    $result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result) == 0) die("There is no image.");

    echo "<br /> '$query' <br />";
    echo "<br /> '$result' <br/ > \n";    

    $row       = mysql_fetch_array($result);     
    $content = $row['image'];
//  $content = mysql_real_escape_string($row[image]); 
//  header("Content-type: image/jpg");
//  echo $content ;     
    echo "<img src=\"!retrieve.php?id='id'\" width=\"250\" height=\"180\" >";    

/*  while ($row = mysql_fetch_array($result)) { 
        extract($row);  
        echo "<img src=!retrieve.php?id='id' width=200 height=150 border=0>";    
        echo $image;        }       */

end quote.

This is an interesting subject, I have played around with trying to store images in the db but I have always had issues with it. If I found a good way to accomplish it, I might use it in my projects but since I haven't I always end up using the other method of just using PHP to physically move the file to a storage folder and store certain pieces of information about the file into the DB.

One question,

One this line, $query = "INSERT INTO blob_eg.upload, what does blob_eg.upload refer to? Is that just a shortcut to be able to refer to the table and table field you want to perform the insert on?

Ex. INSERT INTO table.field VALUE...

@ /\/\ongoose - it's the same as

INSERT INTO database.table_name(column1, column2, column3,...) VALUES (value1, value2, value3,....)

I have way too many random databases and tables with random names, and it helps me keep track of which i'm using.

Property u mean width and height etc?

The script to upload image in some folder is

/*------ html input filed */

<input name="uploadedfile" type="file" />

--------------------------------------------

/*----PHP script----*/

$target_path = "yourTargetFolder"; //give here full path of your target folder where u want to upload your image

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))

{

}

after this script your image will be in targeted folder.

For details information read this

http://www.w3schools.com/php/php_file_upload.asp

Thanks again xuqi - that was very helpful.

By property i mean a building - Each image will be associated with a building, so searchable by building id. That's why i wanted to save in a database. My question was - can the paths to images saved in mysql still be used for that? I.e, each path associated to a building.

As for displaying images - no luck :( i have tried different methods (1) uploading to a folder and displaying, (2) uploading to a db and displaying, and (3) uploading to a folder and saving path in db.

So far, only (1) works flawlessly. (2) I can upload and display, but whatever i try i still get a some jibbersih code as output. I am working on (3) now. I can save images to folders and save their paths to db, and about to get on with retrieving and displaying images from paths.

This works perfectly ... thank you all for your input.

include ("database.php");

	if (!isset($_GET['id'])) { echo "<br />ERROR: id not specified ";    } 
 
	$id = (int) $_GET['id'];
 
	if ($id <= 0) { echo "<br />ERROR: Invalid id <br />"; } 
 
	$query  = sprintf("SELECT * FROM property_images JOIN property ON property.id = property_images.property_id ORDER BY property_images.id DESC LIMIT 1");	
	$result = mysql_query($query, $db) or die('Error, query failed'); 
	
	if (mysql_num_rows($result) == 0) { echo "Error: property has no images. ";   } 

	$row = mysql_fetch_array($result);			
	extract($row);

Anybody please help in PHP Website...

Sir, I am making a PHP website about heroes of Indian Independence like bhagat singh, Ram Prasad Bismil, etc. But problem is when I am showing a image from database in PHP site all images of heroes is display in one site. I want to show only one image like when I giving information about bhagat singh it want to show only bhagat singh image but its show all images like gandhi, nehru,etc. Plz Help, give me some suggestion how to show only one image what I want to show.

@PranayPravin - Start a new thread with your issue (include code and error messages) instead of bumping up an 8 year old thread

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.