Albzi
Albzi

Reputation: 15609

type='file' submits as POST

I have this html:

<form method="post" action="install.php">
  <input type="text" name="database_username" id="database_username">
  <input type="password" id="database_password" name="database_password">
  <input type="file" class="logo-field" id="logo_field" name="file">
  <input type="submit" class="button success expand">
</form>

My PHP in install.php:

if ($_FILES['file']['error'] !== UPLOAD_ERR_OK){
  die("Didn't upload properly. Error: " . $_FILES['file']['error']);
}

$info = getimageSize($_FILES['file']['tmp_name']);

if ($info == FALSE){
  die("Can't get the type of uploaded file.");
}

if (($info[2] !== IMAGE_GIF) && ($info[2] !== IMAGE_JPEG) && ($info[2] !== IMAGE_PNG)){
  die("Not a gif/jpeg/png");
}

However, it isn't working (giving a blank error as per the first die()).

When I var_dump($_FILES) I get:

array(0) { }

But when I do `var_dump($_POST); ?>

array(3) { ["database_username"]=> string(8) "username" ["database_password"]=> string(8) "password" ["file"]=> string(14) "image-name.jpg" }

It's just a normal form which a user will input their username/password and upload an ID if they want, however I'm having a really hard time trying to get the image to go onto the next page.

I get this error when trying to access $_FILES['file']:

Message: Undefined index: file

Q: Why is the image upload not working?

Upvotes: 0

Views: 38

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You are missing

enctype="multipart/form-data" 

in the form.

The form should be as

<form method="post" action="install.php" enctype="multipart/form-data" >

Why its needed ? check here

What does enctype='multipart/form-data' mean?

Upvotes: 4

Related Questions