I'm working out a simple image upload script that will upload and show the image on the same page. It worked perfect until I decided to store the file into folder, instead of the database as a BLOB.
I need to store the image URL path into the database as a VARCHAR and that's the only thing that actually works here. I can see the path stored into the DB, but there is no image in the folder, nor it's shown on the page. Some guidance here will be really appreciated, thank you! :)
Here's the code for index.php and get.php which suppose to show the uploaded image. But I need the direct path to the image, with, for an example .jpg extension.
Code for index.php
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image">
<input type="submit" value="Upload">
</form>
<?php error_reporting(E_ALL ^ E_NOTICE);
// connect to database
mysql_connect ("localhost", "root", "") or die (mysql_error());
mysql_select_db ("dbimage");
// file properties
$file = $_FILES['image']['tmp_name'];
// checking if the file is submitted
if (!isset ($file))
echo "Please select an image";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES ['image']['name']);
$image_size = getimagesize ($_FILES ['image']['tmp_name']);
$image_path = "/imgs/".basename($image_name); // File destination
if ($image_size == FALSE)
echo "That's not an image";
else
{
if(move_uploaded_file($image_name, $image_path))
echo "File uploaded";
if (!$insert = mysql_query ("INSERT INTO store VALUES ('', '$image_name', '', '$image_path')"))
echo "Problem uploading image";
else
{
$lastid = mysql_insert_id ();
echo "Image uploaded! <p>View image:</p> <img src=get.php?id=$lastid>";
}
}
}
?>
get.php code
<?php error_reporting(E_ALL ^ E_NOTICE);
mysql_connect ("localhost", "root", "") or die (mysql_error());
mysql_select_db ("dbimage");
$id = addslashes ($_REQUEST['id']);
$image = mysql_query ("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header ("Content-type: image/jpeg");
echo $image;
?>
Thank you!


Sign In
Create Account


Back to top










