【发布时间】:2014-01-21 15:55:08
【问题描述】:
我正在为一个大学项目制作一个食谱网站,并且正在编写一个上传页面,以便人们可以上传自己的食谱,其中一个选项允许他们上传图片以配合食谱。
我已成功完成实际上传工作,并将图像路径插入到我的数据库中。 当我尝试打印图像时出现问题,我收到一个 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>";}
?>
【问题讨论】:
-
看起来您正在将图像移动到 /uploaded_images/ 文件夹并从 / 显示。您的 /uploaded_images 是否设置为文档根目录?或者,也许您应该尝试将 img src 设置为 /uploaded_images/$n ?
-
该文件夹是图像所在的位置,但我不确定如何设置?
-
您可能想先了解如何指定正确的相对路径。图像文件位于您的 php 脚本所在的位置?除非您的 php 文件位于 /uploaded_images 中,否则您需要指定相对于 php 脚本的文件夹。在写入数据库之前将该相对文件夹路径放入 $n
-
上传的图像文件夹与 php 文件位于同一文件夹中,并且图像在其中,因此为了使文件夹路径相对,我必须这样做 //uploaded_images/$n?
-
你的 move_uploaded_file 函数中有什么?
标签: php mysql image forms image-uploading