I have a html form like so:
<form method="post" enctype="multipart/form-data">
<input type="file" name="pictures[]" required>
<input type="file" name="pictures[]">
<input type="file" name="pictures[]">
<input type="file" name="pictures[]">
<input type="file" name="pictures[]">
</form>
such that, a user can upload up to 5 pictures. I know that I can do it with <input type="file" name="pictures[]" multiple>, but the idea is that, I want the users to be able to select files from different folders of their device(s), hence the reason why I wrote in the format above.
Now, the challenge I am having now is that, not all the inputs are required, and when the form is submitted, and I do the normal input checks $_FILES['pictures']['name], it loops through all the file array, which is normal since they have the same name.
Now my headache..
- How do I validate only the inputs that a file is entered while maintaining the one required field?
For now, if I do a check $_FILES['pictures']['name'] when less than 5 files are added in the input boxes, it returns an error because of the input field(s) that were not entered.
EDIT
First I check if at least one file was entered, then I check if any of the files do not return any error. See my code below:
if ( count($_FILES['pictures']['name']) < 1 ) {
echo "Please enter at least one image";
}
elseif ( $_FILES['pictures']['error'] != 0 ) {
echo "Sorry, one or more of your uploaded images is invalid";
}
Now the form will always return an error if not all the 5 inputs fields are entered.
ANOTHER EDIT
Maybe I wasn't able to explain clearly what I wanted to achieve.. but here's the gist.
Apart from the fact that one of the inputs is required, when the form is submitted without all the fields entered, and I do something like so (skipping the required check):
$files = array();
$fileData = $_FILES['pictures'];
$files = array();
if ( is_array($fileData['name']) ) {
for ( $i=0; $i < count($fileData['name']) ; ++$i ) {
$files[] = array(
'name' => $fileData['name'][$i],
'tmp_name' => $fileData['tmp_name'][$i],
'type' => $fileData['type'][$i],
'error' => $fileData['error'][$i],
'size' => $fileData['size'][$i],
);
}
}
else {
$files[] = $fileData;
}
So when I perform validations like:
foreach ($files as $file) {
if ( $file['name'] == '' ) {
echo "Error";
}
}
, and the user did not enter all the images, it returns the error message.
A print_r() of the $files returns this:
Array
(
[photos] => Array
(
[name] => Array
(
[0] => 11356010_127900540886840_1152019271_n.jpg
[1] => 11370980_130679033939470_1067474802_n.jpg
[2] =>
[3] =>
[4] =>
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] =>
[3] =>
[4] =>
)
[tmp_name] => Array
(
[0] => C:\xampp7\tmp\php1C9F.tmp
[1] => C:\xampp7\tmp\php1CaF.tmp
[2] =>
[3] =>
[4] =>
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 4
[3] => 4
[4] => 4
)
[size] => Array
(
[0] => 108492
[1] => 108492
[2] => 0
[3] => 0
[4] => 0
)
)
[files] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
Is there a way I can make the empty fields to be ignore when doing my checks?
I know I can do this with javascript by checking for the empty fields and add the attribute of disabled on submit, but what if the user turned off javascript in his/her browser, what happens?
I've tried using array_filter() like so:
$filtered = array_filter($_FILES['pictures'], function($var){
return !empty($var['pictures']['name']);
});
, but it returns an empty array. Can someone please point me to the right direction?
namekey is not empty. If something is inputed and uploaded successfully thenerrorkey is 0.name, it returns an error, because of the other input that was not entered.