Undefined index in file while uploading image

2019-09-20 11:45发布

I am trying to create a form where a user can upload images. I'm using php for validation of this file to see whether it is an image file or not but I am getting an error

"Undefined index file.."

I can't understand what's wrong.. Please help

HTML code..

<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 code...

  <?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";
  }
  ?>

1条回答
爷、活的狠高调
2楼-- · 2019-09-20 12:25

There were two things wrong with your handler.

1) There was a missing closing brace above your last else condition

2) The following line contained characters (dots in tmp...) that didn't belong:

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

Which was changed to:

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

Reformatted code, tested

<?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";
  }

?>

Added bonus: I added a few echo "<br>"; to seperate the fields on successful upload.

Which will appear like this:

Upload: image_test.jpg
Type: image/jpeg
Size: 26.16015625 Kb
Temp file: /tmp/phpifKd7I
Stored in: users/image_test.jpg

instead of on one line.

查看更多
登录 后发表回答