Output image using path in Database??

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
midnite
Forum Newbie
Posts: 14
Joined: Sun Nov 06, 2011 2:58 pm

Output image using path in Database??

Post by midnite »

Hi there guys,

i've got this script that lets the user upload an image with a title, then stores the image path in a database and then retrieves the image and the image title using the path and the title stored in the database. Unfortunately i'm able to do everything except output the image and the title. All i want is to display the existing images and all the future uploaded images. Could anyone give me a little help please.

Here is the code:

config.php

Code: Select all

<?php

$config['app_dir'] = dirname(dirname(__FILE__));
 

$config['upload_dir'] = $config['app_dir'] . '/images/';

?>
index.php

Code: Select all

//html form
<html>
<head>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    <p>Please Enter the Image Title.</p>
	<input type="text" name="title"/>
	
	<p>Please Select an Image2</p>
	<input type="file" name="image"> 
	
	<input TYPE="submit" name="upload" title="Upload Image" value="Upload"/>
</form>
<body>
</html>

 <?php 
if(isset($_POST['upload'])) {

	require_once dirname(__FILE__).'/includes/config.php';

	$dir = $config['upload_dir']; 
	$target = $dir . basename( $_FILES['image']['name']); 

	//This gets all the other information from the form 
	$title = $_POST['title']; 
	$image = $_FILES['image']['name']; 

	$tmp = $_FILES['image']['tmp_name'];

	
	
	//database connection, change to mysqli and store in a different page 
	mysql_connect("host", "username", "password") or die(mysql_error()) ;
	mysql_select_db("database") or die(mysql_error()) ;

	//insert into the 'images' table 
        //i've also tried to insert the path inside the table but i've obtained the same result.
	mysql_query("INSERT INTO `images` VALUES ('', '$title', '$image')") ; 
	 
	 //writes the image to the server 
	 if(move_uploaded_file($tmp, $target)) 
	 { 
	 
		 //if its all ok
		 echo "The file ". $image. " has been uploaded, and your information has been added to the directory"; 
	 } 
	 else { 
	 
		 //else if its not ok
		 echo "Sorry, there was a problem uploading your file."; 
	 } 
	 
	 //retrieves data from mysql
	 $data = mysql_query("SELECT * FROM images") or die(mysql_error());
	 
         //using firebug inspect element i can see that its displaying the right path, but it does not displays the image
         //all the folders in the server were set to 777 and still nothing
	 while($info = mysql_fetch_array($data)) {
	 
		echo '<h3>'.$info['title'].'</h3>';
		echo "<p><img src=".$dir.$info['image_name']."></p>";
	}
}
 ?> 

Thanks for the help guys.
midnite
Forum Newbie
Posts: 14
Joined: Sun Nov 06, 2011 2:58 pm

Re: Output image using path in Database??

Post by midnite »

Hi Guys,

Thanks for the help but i've just managed to fix this, the issue was that i was outputting the image with full path, so i got rid of the config file and replaced the following:

index.php

Code: Select all

//replace this
//require_once dirname(__FILE__).'/includes/config.php';

//$dir = $config['upload_dir']; 
//$target = $dir . basename( $_FILES['image']['name']);
//


//for this
$target = 'images/' . $image;


//and this
//echo "<p><img src=".$dir.$info['image_name']."></p>"; 
//

//for this
echo "<p><img src=".$info['path']."></p>"; 
Maybe this could help someone, who knows.

Thanks anyway and sorry for bothering you.
Post Reply