0

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?

Albzi
  • 15,431
  • 6
  • 46
  • 63

1 Answers1

4

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?

Community
  • 1
  • 1
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • Thanks for the quick reply, however now it gives me this error: `Didn't upload properly. Error: 1`. The `1` being from `$_FILES['file']['error']` – Albzi Apr 11 '14 at 14:56
  • check your file size its probably crossing the limit specified on php.ini upload_max_filesize – Abhik Chakraborty Apr 11 '14 at 14:59
  • Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini. – John Conde Apr 11 '14 at 15:00
  • Thanks guys. However, I'm going to be distributing this code to people, will they also need to edit their `php.ini`? @JohnConde – Albzi Apr 11 '14 at 15:02
  • Yes in deed. They have to if you are uploading file greater than the specified one in php.ini the value has to be changed else it will not work. You can mention in a readme file about the limitation !! – Abhik Chakraborty Apr 11 '14 at 15:04
  • Only if they want to upload files larger then their current settings – John Conde Apr 11 '14 at 15:04