【问题标题】:PHP File upload form not workingPHP文件上传表单不起作用
【发布时间】:2011-09-05 13:47:15
【问题描述】:

我已经在我创建的另一个站点中使用了此代码,并且它工作得非常好,所以我真的很困惑为什么它不能正常工作。我唯一更改的是要移动到的文件的目录,但文件永远不会上传。我已经三重检查了一切,一切似乎都很好。代码全部执行没有错误,所有信息都正确输入到数据库中,唯一的问题是文件没有上传。我要疯了,试图让它工作!请帮忙!

HTML 表格:

<p>
<h2>Add News</h2><br />
REQUIRED *<br /><br />
<form enctype="multipart/form-data" action='addnews2.php' method='post' onsubmit="chose.value = uploadForm()" name="newsform">
<p>Headline:  *
<br>
<input name="title" type="text" size="75">
<br />
<br/>

  News: *<br>
<textarea name="text" cols="75" rows="10" width="200"></textarea><br /><br />
Image File:   
<br>
<input type="file" name="filetoupload" id="filetoupload"><br/><br />

  <input type="submit" name="choose" value="Submit" />
</p>
</form>
</p>

处理页面:

$date=date("l F d, Y");

$title=stripslashes($_POST['title']);
$text=stripslashes($_POST['text']);

if (($_FILES["filetoupload"]["type"] == "image/gif")
|| ($_FILES["filetoupload"]["type"] == "image/jpeg")
|| ($_FILES["filetoupload"]["type"] == "image/pjpeg")
|| ($_FILES["filetoupload"]["type"] == "image/png")
|| ($_FILES["filetoupload"]["type"] == "image/jpg"))
  {
  if ($_FILES["filetoupload"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["filetoupload"]["error"] . "<br />";
    }
    else
      {
            if (file_exists("images/news/" . $_FILES["filetoupload"]["name"]))
      {
      echo $_FILES["filetoupload"]["name"] . " already exists. ";
      }
      move_uploaded_file($_FILES["filetoupload"]["tmp_name"],
      "images/news/" . $_FILES["filetoupload"]["name"]);
      }
    }



$image = "images/news/".$_FILES['filetoupload']['name'];


if($title==""||$text==""){
    die("You need to fill in all details");
}


connect_to_db();
$query="insert into news (date, title, text, image) values ('".$date."','".mysql_real_escape_string($title)."','".mysql_real_escape_string($text)."','".$image."')";
$result=mysql_query($query);

if(mysql_affected_rows()==1){
header("Location:index.php?page=Admin&news=added");
}
else{
    die("there was a problem");
}
mysql_close();

【问题讨论】:

  • 您是否检查了您要上传到的目录的权限?
  • 你的 move_uploaded_file 代码总是在执行,你需要把它放在 else 部分
  • 为了追踪问题,您应该构建一个没有数据库信息的新临时 php 文件,然后收集并回显所有变量的信息,以确保您的位置、路径和构造的数据有效.同时启用错误报告 (php.net/manual/en/function.error-reporting.php)。此外,如上所述,最终副本的目标位置可能没有适当的文件权限(写入权限)。

标签: php forms file upload


【解决方案1】:

看起来您可能缺少 else 语句,代码格式已关闭并导致混乱:

  if (file_exists("images/news/" . $_FILES["filetoupload"]["name"]))
  {
      echo $_FILES["filetoupload"]["name"] . " already exists. ";
  }else {
      move_uploaded_file($_FILES["filetoupload"]["tmp_name"],
  "images/news/" . $_FILES["filetoupload"]["name"]);
  } 

试一试。如果不是这样,请检查您的权限等。并确保文件没有被上传,只是不在您预期的位置。

只是一点信息,我会在您进行图像检查之前检查$title$text。因为如果两者都为空,您可能不想将图像上传到站点。您还应该在插入之前转义图像路径中的文件名:

$image = "images/news/".mysql_real_escape_string($_FILES['filetoupload']['name']);

因为这也可能容易注入。

【讨论】:

  • 感谢您的快速响应。不幸的是,这并没有解决问题:/
  • 我才意识到问题所在。我没有给文件夹写权限。我真是个 eeeejot!但是你的回答引发了这个想法,非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-11
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多