【发布时间】: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)。此外,如上所述,最终副本的目标位置可能没有适当的文件权限(写入权限)。