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']."
";
}
Even easier is to use a foreach loop:
foreach($_FILES['files'] as $file) {
if($file['size'])
echo $file['name']."
";
}
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
}
You can loop through the files in the following way:
$count = count($_FILES['files']['tmp_name']);
for($i=0;$i<$count;$i++) {
$file_name = $_FILES['files']['name'][$i];
$file_type = $_FILES['files']['type'][$i];
$file_error = $_FILES['files']['error'][$i];
$file_size = $_FILES['files']['size'][$i];
}
So as I understood from your question,$_FILES['files']
is array of uploaded file ,so you can act it like array. try foreach
foreach ($_FILES['files'] as $file){
// now do something with $file
move_uploaded_file($file["tmp_name"],"upload/" . $file["name"]);//e.g uploading it
}