我正在为一个大学项目制作食谱网站,我正在编写一个上传页面,以便人们可以上传他们自己的食谱,其中一个选项允许他们上传图像以配合食谱。
我已设法使实际上传工作,并将imagepath插入我的数据库。 当我尝试打印图像时出现问题,我收到404错误,告诉我无法找到图像,虽然我不理解这一点,因为我可以在浏览器中导航到图像。
以下是上传页面的代码
<?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);
}
?>
编辑这里是要打印的代码:
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>";}
?>
答案 0 :(得分:0)
我刚刚解决了这个问题,我必须将结果页面上的输出更改为:
$mealpic .="<img src='http://ml11maj.icsnewmedia.net/Workshops/Week%207/".$row['imagepath']."'/>";
这
$mealpic .="<img src=".$row['imagepath']."/>";