Php 找不到Imagepath的Url
我正在为一个大学项目制作一个食谱网站,并且正在编写一个上传页面,这样人们就可以上传自己的食谱,其中一个选项允许他们上传一张与食谱相关的图片 我已经设法使实际的上传工作,并插入一个imagepath到我的数据库。 当我尝试打印图像时,出现了一个404错误,告诉我找不到图像,尽管我不理解这一点,因为我可以在浏览器中导航到图像 这是上传页面的代码Php 找不到Imagepath的Url,php,mysql,image,forms,image-uploading,Php,Mysql,Image,Forms,Image Uploading,我正在为一个大学项目制作一个食谱网站,并且正在编写一个上传页面,这样人们就可以上传自己的食谱,其中一个选项允许他们上传一张与食谱相关的图片 我已经设法使实际的上传工作,并插入一个imagepath到我的数据库。 当我尝试打印图像时,出现了一个404错误,告诉我找不到图像,尽管我不理解这一点,因为我可以在浏览器中导航到图像 这是上传页面的代码 <?php require_once ("checklog.php"); require_once ("function.php"); include
<?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);
}
?>
我刚刚解决了这个问题,我不得不将结果页面上的输出更改为:
$mealpic .="<img src='http://ml11maj.icsnewmedia.net/Workshops/Week%207/".$row['imagepath']."'/>";
$mealpic.=”;
从
$mealpic.=”;
看起来您正在将图像移动到/upload\u images/文件夹,并从/显示。您上传的图片是否设置为文档根目录?或者您应该尝试将img src设置为/uploaded_images/$n?该文件夹就是图像所在的位置,但我不确定如何设置?您可能需要首先了解如何指定正确的相对路径。图像文件位于php脚本所在的位置?除非您的php文件位于/uploaded_图像中,否则您需要指定相对于php脚本的文件夹。在写入数据库之前,将该相对文件夹路径放入$n。上载的图像文件夹与php文件位于同一文件夹中,并且图像位于该文件夹中。因此,为了使文件夹路径相对,我必须执行//上载的图像/$n?move\u上载的文件函数中有什么内容?这是绝对路径,很高兴它最终能够工作。您可能需要花一些时间来弄清楚如何在HTML中指定文件路径。
$mealpic .="<img src='http://ml11maj.icsnewmedia.net/Workshops/Week%207/".$row['imagepath']."'/>";
$mealpic .="<img src=".$row['imagepath']."/>";