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