I want to add loop around $_FILES[] array. I tried $count= count($_FILES['files'][name])
and then add loop around it for($i=0; $i<=$count; $i++)
, but it is not giving me desired output.
I want add to loop only from the files that the User is uploading, Right now it is counting all of the 'files' available in the form.
Kindly tell me a way to count only the uploaded files and loop through them.
Loop ever the $_FILES
array and use the ['name']
inside the iteration:
$count = count($_FILES['files'])
for($i=0; $i<=$count; $i++) {
if ($_FILES['files'][$i]['size'])
echo $_FILES['files'][$i]['name']."\n";
}
Even easier is to use a foreach loop:
foreach($_FILES['files'] as $file) {
if($file['size'])
echo $file['name']."\n";
}
You can use array_filter for that. You filter the array to just get the inputs with uploaded files in them.
$uploaded_files = array_filter($_FILES['files'], function($file){
return $file['size'];
});
print count($uploaded_files);
foreach($uploaded_files as $file)
{
//code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With