0

我正在尝试将图像上传到我的服务器,现在代码非常简单,但它从未通过 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";
  }

任何帮助将不胜感激。

提前致谢...

4

2 回答 2

3

要上传文件,您的表单方法应始终为 POST,因此在表单方法中将 GET 转换为 POST

于 2012-08-16T10:57:38.783 回答
0

检查您要上传的文件类型。使用:

echo $_FILES["image1"]["type"];

因为有时我们尝试上传的图像类型不同。或者您可以尝试其他图像。

于 2012-08-16T11:16:31.837 回答