0

When I try to upload a text file I get 'Undefined index: UsernamesInput in'

I tried lookin up the issue I found some examples but I they didn't make any sense to my specific code

This is my HTML code:

     <div class="container forms">
           <form action="verify.php" method="POST">
            <div class="col-sm-4">
                <fieldset class="form-group">
                    <label for="exampleTextarea">Emails</label>
                    <textarea class="form-control" id="exampleTextarea" rows="5" placeholder="Emails here..."></textarea>
                </fieldset>
                <fieldset class="form-group middle">
                    <label for="exampleInputFile">Email input</label>
                    <input type="file" class="form-control-file" id="exampleInputFile">
                    <small class="text-muted">Upload the E-mail adresses for registering</small>
                </fieldset>
            </div>
            <div class="col-sm-4">
                <fieldset class="form-group">
                    <label for="exampleTextarea">Passwords</label>
                    <textarea class="form-control" id="exampleTextarea" rows="5" placeholder="Passwords here..."></textarea>
                </fieldset>
                <fieldset class="form-group middle">
                    <label for="exampleInputFile">Password input</label>
                    <input type="file" class="form-control-file" id="exampleInputFile">
                    <small class="text-muted">Upload the Passwords for registering</small>
                </fieldset>
            </div>
            <div class="col-sm-4">
                <fieldset class="form-group">
                    <label for="exampleTextarea">Usernames</label>
                    <textarea class="form-control" id="exampleTextarea" rows="5" placeholder="Usernames here..."></textarea>
                </fieldset> 
                <fieldset class="form-group middle">
                    <label for="exampleInputFile">Username input</label>
                    <input type="file" name="UsernamesInput" class="form-control-file" id="exampleInputFile">
                    <small class="text-muted">Upload the Usernames for registering</small>
                </fieldset>
            </div>
            <input type="submit" name="submit" value="Create Accounts!" class="btn btn-info btn-block">
           </form>
     </div>

and my PHP code:

            <?php

            $_SERVER['REQUEST_METHOD'];

            $name      = $_FILES['UsernamesInput']['name'];
            $extention = strtolower(substr($name, strpos($name, '.') + 1));
            $type      = $_FILES['UsernamesInput']['type'];

            //$size = $_FILES['UsernamesInput']['size'];


            $tmp_name = $_FILES['UsernamesInput']['tmp_name'];

            if (isset($name)) {
                if (!empty($name)) {

                    if (($extention == 'txt') and ($type == 'text/plain')) {

                        $location = 'uploads/';

                        if (move_uploaded_file($tmp_name, $location . $name)) {
                            echo 'Uploaded!';
                        } else {
                            echo 'There was an error';
                        }

                    } else {
                        echo 'Error, file must be in text format.';
                    }
                }
            }



            ?>

Thanks in advance,

Naomi
  • 271
  • 1
  • 3
  • 15

2 Answers2

0

Add name for each type='file' inputs and also try adding enctype="multipart/form-data in the form

 <form action="verify.php" method="POST" enctype="multipart/form-data">
 <input type="file" name="UsernamesInput" class="form-control-file" id="exampleInputFile">
 </form>
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
0

You need to add enctype="multipart/form-data" in your HTML form becuase you are using input type file in your HTML.

<form action="actionFile" method="post" enctype="multipart/form-data">

HTML Form Enctype

devpro
  • 16,184
  • 3
  • 27
  • 38