-1

我正在尝试创建一个用户可以上传图像的表单。我正在使用 php 验证此文件以查看它是否为图像文件,但出现错误

“未定义的索引文件..”

我不明白出了什么问题..请帮忙

HTML 代码..

<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="photo" id="file" /> 
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

PHP代码...

  <?php
  if ((($_FILES["photo"]["type"] == "image/gif")
  || ($_FILES["photo"]["type"] == "image/jpeg")
  || ($_FILES["photo"]["type"] == "image/png"))
  && ($_FILES["photo"]["size"] < 1000000))
  {
  if ($_FILES["photo"]["error"] > 0)
  {
  echo "Return Code: " . $_FILES["photo"]["error"] . " ";
  }
  else
  {
  echo "Upload: " . $_FILES["photo"]["name"] . "";
  echo "Type: " . $_FILES["photo"]["type"] . "";
  echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " Kb";
  echo "Temp file: " . $_FILES["photo"]["tmp_name"] . "";

  if (file_exists("users/" . $_FILES["photo"]["name"]))
  {
  echo $_FILES["photo"]["name"] . " already exists. ";
  }
  else
  {
  move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
  echo "Stored in: " . "users/" . $_FILES["photo"]["name"];
  }
  }
  else
  {
  echo "Invalid file";
  }
  ?>
4

1 回答 1

1

您的处理程序有两个问题。

1)您的最后一个else条件上方缺少右括号

2) 以下行包含tmp...不属于的字符(中的点):

move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);

更改为:

move_uploaded_file($_FILES["photo"]["tmp_name"], "users/" . $_FILES["photo"]["name"]);

重新格式化的代码,经过测试

<?php
  if ((($_FILES["photo"]["type"] == "image/gif")
  || ($_FILES["photo"]["type"] == "image/jpeg")
  || ($_FILES["photo"]["type"] == "image/png"))
  && ($_FILES["photo"]["size"] < 1000000))
  {
  if ($_FILES["photo"]["error"] > 0)
  {
  echo "Return Code: " . $_FILES["photo"]["error"] . " ";
  }
  else
  {
    echo "Upload: " . $_FILES["photo"]["name"] . "";
    echo "<br>";
    echo "Type: " . $_FILES["photo"]["type"] . "";
    echo "<br>";
    echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " Kb";
    echo "<br>";
    echo "Temp file: " . $_FILES["photo"]["tmp_name"] . "";
    echo "<br>";

  if (file_exists("users/" . $_FILES["photo"]["name"]))
  {
  echo $_FILES["photo"]["name"] . " already exists. ";
  }
  else
  {

// error line for you to compare the error
// move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);

move_uploaded_file($_FILES["photo"]["tmp_name"], "users/" . $_FILES["photo"]["name"]);

  echo "Stored in: " . "users/" . $_FILES["photo"]["name"];
  }
  }
  } // this was the missing closing brace
  else
  {
  echo "Invalid file";
  }

?>

额外奖励:我添加了一些echo "<br>";以分隔成功上传的字段。

这将如下所示:

上传:image_test.jpg
类型:image/jpeg
大小:26.16015625 Kb
临时文件:/tmp/phpifKd7I
存储在:users/image_test.jpg

而不是一行。

于 2013-09-19T12:30:53.860 回答