Output image using path in Database??
Posted: Sat Aug 11, 2012 12:43 pm
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
index.php
Thanks for the help 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/';
?>
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.