3

I am trying to allow my users to upload multiple files at one time. I have set up my form to allow multiples I believe; here is the code for that, so someone can help me to make sure that it is correct:

<form class="well" action="" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="file">Add Documents Such As (Certifications, Degrees, Prior Job Offers, Etc) Here:</label><br>
        <label for="file">Select a file to upload</label>
        <input type="file" name="filesToUpload" id="filesToUpload" multiple>
    </div>
    <button class="glyphicon glyphicon-open-file" type="submit" name="submit"/>Submit
</form>

Here is the code that I am attempting to use to get the selected files uploaded to my server:

if (isset($_POST['submit'])) {
    $file = $_FILES['filesToUpload'];
    foreach ($_FILES['filesToUpload'] as $file) {
        $fileName = $file['name'];
        echo $fileName;
        $fileTmpName = $file['tmp_name'];
        $fileSize = $file['size'];
        $fileError = $file['error'];
        $fileType = $file['type'];

        $fileExt = explode('.', $fileName);
        $fileActualExt = strtolower(end($fileExt));

        $allowed = array('pdf', 'docx', 'doc', 'jpg', 'jpeg', 'txt', 'png');
        if (in_array($fileActualExt, $allowed)) {
            if ($fileError === 0) {
                if ($fileSize < 1000000) {
                    $fileNameNew = uniqid('', true).".".$fileActualExt;
                    $fileDestination = 'uploads/'.$fileNameNew;
                    move_uploaded_file($fileTmpName, $fileDestination);
                } else {
                    echo "Your file is too big!";
                  }
            } else {
                echo "There was an error uploading your file!";
              }
        } else {
            echo "You cannot upload files of this type!";
          }
    }
    header("Location: developer");
}

The thing I have noticed, when I echo $file after I have selected multiple files it only shows the name of one of the files. I am not sure, but I have the feeling that is why I can only get one file to upload to the server when trying to upload multiple files.

Can someone confirm if this is the issue? If it is, what would be the best way to go about getting this working.

Edit: I have tried all the other suggestions that have been made around the site here. When I use <input type="file" name="filesToUpload[]" id="filesToUpload" multiple> it just breaks it. I get all these notices about undefined index for the I guess you call them extensions (based on what someone said below) when trying to break out the different parts of the file. I also get a notice about Array to String conversion. Using the brackets in the input also just jumps to the else statement about not being able to upload files of a certain type.

Here they are:

Notice: Array to string conversion in /var/www/html/dev/my-documents/upload-document.php on line 47
Array
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
You cannot upload files of this type!
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
You cannot upload files of this type!
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
You cannot upload files of this type!
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
You cannot upload files of this type!
Notice: Undefined index: name in /var/www/html/dev/my-documents/upload-document.php on line 49

Notice: Undefined index: tmp_name in /var/www/html/dev/my-documents/upload-document.php on line 51

Notice: Undefined index: size in /var/www/html/dev/my-documents/upload-document.php on line 52

Notice: Undefined index: error in /var/www/html/dev/my-documents/upload-document.php on line 53

Notice: Undefined index: type in /var/www/html/dev/my-documents/upload-document.php on line 54
7
  • multiple only works where html5 is supported, does your browser support it? Commented Nov 14, 2017 at 3:42
  • RTM: php.net/manual/en/features.file-upload.multiple.php Commented Nov 14, 2017 at 3:44
  • And stackoverflow.com/questions/14007243/… Commented Nov 14, 2017 at 3:51
  • fyi, the way you check for extensions is wrong and can easily be spoofed. you should be checking the mime type. Hint finfo for php 5.3+ Commented Nov 14, 2017 at 4:01
  • 1
    Don't be confused - getting "all these notices" is forward progress. You've just failed to properly iterate over the entire array of files. I've added an answer to the marked duplicate using a stripped down version of your code. Good luck. Ping me here (@HPierce) if anything was unclear. Commented Nov 15, 2017 at 1:33

2 Answers 2

2
 You should 
 <input type="file" name="filesToUpload" id="filesToUpload" multiple> instead of 
<input type="file" name="filesToUpload[]" id="filesToUpload" multiple> 
Array format 

for more info visit [https://stackoverflow.com/questions/2704314/multiple-file-upload-in-php]
Sign up to request clarification or add additional context in comments.

Comments

2
<input type="file" name="filesToUpload" id="filesToUpload" multiple>

Its should be array. change the above code as below

<input type="file" name="filesToUpload[]" id="filesToUpload" multiple>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.