2

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
DeusMach
  • 33
  • 4
  • `multiple` only works where html5 is supported, does your browser support it? – Kisaragi Nov 14 '17 at 03:42
  • RTM: http://php.net/manual/en/features.file-upload.multiple.php – j08691 Nov 14 '17 at 03:44
  • And https://stackoverflow.com/questions/14007243/multiple-file-uploader-only-sends-one-file – j08691 Nov 14 '17 at 03: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+` – Rotimi Nov 14 '17 at 04:01
  • 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](https://stackoverflow.com/a/47297968/3000068) to the marked duplicate using a stripped down version of your code. Good luck. Ping me here (@HPierce) if anything was unclear. – HPierce Nov 15 '17 at 01:33
  • Thank you @HPierce. I appreciate the help. – DeusMach Nov 15 '17 at 02:38
  • @Akintunde after I get the information for finfo, do I use it the same as I had? – DeusMach Nov 15 '17 at 02:40

2 Answers2

1
 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]
Yagnesh bhalala
  • 1,107
  • 1
  • 15
  • 17
1
<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>
Nandhi Kumar
  • 343
  • 1
  • 11