I am making a recipe website for a university project, and am writing an upload page so that people can upload their own recipe's, one of the options allows them to upload an image to go with the recipe.
I have managed to make the actual upload work, and to insert an imagepath into my database. The problem arises when I try and print out the image, i get a 404 error telling me the image cannot be found, although I don't understand this as I can navigate to the image in my browser.
Here is the code from the upload page
<?php
require_once ("checklog.php");
require_once ("function.php");
include_once ("home_start_logged.php");
require_once ("db_connect.php");
require_once ("cuisine_dropdown.php");
session_start();
//get form data//
$upload = trim($_POST['Upload']);
$mealname = trim($_POST['mealname']);
$ingredients = trim($_POST['ingredients']);
$hours = trim($_POST['hours']);
$minutes = trim($_POST['minutes']);
$recipe = trim($_POST['recipe']);
$userid = trim($_SESSION['userid']);
$cuisine = trim($_POST['cuisine']);
$meal = trim($_POST['meal']);
$feeds = trim($_POST['feeds']);
$dropoption = trim($_POST['dropoption']);
if(trim($_POST['Submit']) =="Upload"){
//handle submitted data here
//process details here//
if($db_server){
//clean the input now we have a db connection//
$mealname = clean_string($db_server, $mealname);
$ingredients = clean_string($db_server, $ingredients);
$hour = clean_string($db_server, $hour);
$minutes = clean_string($db_server, $minutes);
$recipe = clean_string($db_server, $recipe);
$ingredients = clean_string($db_server, $ingredients);
$userid = clean_string($db_server, $userid);
$cuisine = clean_string($db_server, $cuisine);
$meal = clean_string($db_server,$meal);
$feeds = clean_string($db_server,$feeds);
$dropoption = clean_string($db_server, $dropoption);
mysqli_select_db($db_server, $db_database) ;
//check whether the recipe exists//
$query="SELECT mealname FROM `recipename` WHERE mealname='$mealname'";
$result = mysqli_query($db_server, $query);
if ($row = mysqli_fetch_array($result)){
$message = "Meal already exists. Please try again.";
}else{
//code to process image here//
//put file properties into variable//
if($_FILES) {
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
$tmp_name = $_FILES['image']['tmp_name'];
//determine file type//
switch($_FILES['image']['type']){
case'image/jpeg': $ext ="jpg"; break;
case'image/png': $ext ="png"; break;
default: $ext =''; break;
}
if($ext){
if($size >30000){
$n="$name";
$n= ereg_replace("[^A-Za-z0-9.]","",$n);
$n= strtolower($n);
$n="/uploaded_images/$n";
move_uploaded_file($tmp_name,$n);
echo "<p>Uploaded image'$name' as '$n':</p>";
echo "<img src='$n'/>";
}
else echo "<p>'$name' is too big - 3MB Max(30,000bytes).</p>";
}
else echo "<p>'$name' is an invalid file - only jpg and png accepted.</p>";
}
else echo "<p>No image uploaded. </p>";
if($cuisine=="") {
$query = "INSERT INTO `recipename` (mealname,ingredients,hours,minutes,recipe,imagepath,userid,b_l_d,feeds,cuisine_type) VALUES ('$mealname', '$ingredients','$hours','$minutes','$recipe','$n','$userid','$meal','$feeds','$dropoption')";
mysqli_query($db_server, $query) or
die("Insert failed: " . mysqli_error($db_server)) ;
}else{
$query = "INSERT INTO`recipename`(mealname,ingredients,hours,minutes,recipe,imagepath,userid,b_l_d,feeds,cuisine_type)VALUES('$mealname', '$ingredients','$hours','$minutes','$recipe','$n','$userid','$meal','$feeds','$cuisine')";
mysqli_query($db_server, $query) or
die("Insert failed: " . mysqli_error($db_server)) ;
$query = "INSERT INTO `Nation` (cuisine_type) VALUES ('$cuisine')";
mysqli_select_db($db_server, $db_database);
mysqli_query($db_server, $query) or
die("Insert failed: " . mysqli_error($db_server)) ;
}
}
$message = "<strong>Recipe Uploaded!</strong>";
}
mysqli_free_result($result);
}
?>
EDIT here is the code to print out:
if (!$db_server){
die("unable to Connect to MYSQL: " . mysqli_connect_error($db_server));
$db_status = "not connected";
}else{
if(trim($_POST['submit']) =="submit"){
}else{
if (isset($_POST['dropoption']) && ($_POST['dropoption'] != '')){
if (isset($_POST['meal']) && ($_POST['meal'] != '')) {
$dropoption = clean_string($db_server, $_POST['dropoption']);
$meal = clean_string($db_server, $_POST['meal']);
$query = "SELECT * FROM `recipename` WHERE `cuisine_type` ='$dropoption' AND b_l_d ='$meal'LIMIT 0,1";
mysqli_select_db($db_server, $db_database);
$result=mysqli_query($db_server, $query);
if (!$result) die("database access failed: " . mysqli_error($db_server));
while($row = mysqli_fetch_array($result)){
$recipename .="<h1>". "Why dont you have ".$row['mealname']."</h1>";
$ingredients .="<p>".$row['ingredients']."</p>";
$recipe .="<p>" .$row['recipe']."</p>";
$cookingtime .="<h4>" .$row['hours']." Hours".$row['minutes']." Minutes </h4>";
$mealpic .="<img src=".$row['imagepath']."/>";
}
<body>
<?php echo $recipename;
echo $mealpic;
?>
<h2>Ingredients</h2>
<?php
$ingredientchunks = (explode(",",$ingredients));
for($i = 1; $i < count($ingredientchunks); $i++){
echo "$i.$ingredientchunks[$i] <br/>";}
echo $cookingtime;
?>
<h2>Recipe</h2>
<?php
$recipechunks = (explode(",",$recipe));
for($i = 1; $i < count($recipechunks); $i++){
echo "$i.$recipechunks[$i] </br>";}
?>
I just solved this I had to change the output on the results page to:
$mealpic .="<img src='http://ml11maj.icsnewmedia.net/Workshops/Week%207/".$row['imagepath']."'/>";
from
$mealpic .="<img src=".$row['imagepath']."/>";