我正在尝试将图像上传到我的服务器,现在代码非常简单,但它从未通过if测试,它总是直接进入else语句并在那里执行代码,即;无效的文件。如下表格代码......
<form action="take.php" method="get" onsubmit='return chequer()' enctype="multipart/form-data">
<input type="file" name="image1"/>
</form>
上传图片的php脚本如下,当然是在take.php文件中:
if ($_GET["image"] == Null)
{
$sql = "INSERT INTO postable (description, dated, posterid, country, state, city, area)
VALUES
('$_GET[Desc]',NOW(),'$row[0]','$_GET[Country]','$_GET[State]','$_GET[City]','$_GET[area]')";
}
else {
$sql = "INSERT INTO postable (description, dated, image, posterid, country, state, city, area)
VALUES
('$_GET[Desc]',NOW(),'$_GET[image]','$row[0]','$_GET[Country]','$_GET[State]','$_GET[City]','$_GET[area]')";
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["image1"]["name"]));
if ((($_FILES["image1"]["type"] == "image/gif")
|| ($_FILES["image1"]["type"] == "image/jpeg")
|| ($_FILES["image1"]["type"] == "image/pjpeg"))
&& ($_FILES["image1"]["size"] < 65536)
&& in_array($extension, $allowedExts))
{
if ($_FILES["image1"]["error"] > 0)
{
echo "Return Code: " . $_FILES["image1"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["image1"]["name"] . "<br />";
echo "Type: " . $_FILES["image1"]["type"] . "<br />";
echo "Size: " . ($_FILES["image1"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["image1"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["image1"]["name"]))
{
echo $_FILES["image1"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["image1"]["tmp_name"],
"upload/" . $_FILES["image1"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["image1"]["name"];
}
}
}
else
{
echo "Invalid file";
}
非常感谢任何帮助。
提前致谢...
答案 0 :(得分:3)
要上传文件,您的表单方法应始终为POST,以便将GET转换为使用表单方法
的POST答案 1 :(得分:0)
检查您要上传的文件类型。使用:
echo $_FILES["image1"]["type"];
因为有些时候我们尝试上传的图片类型不同。或者您可以尝试其他图片。