I'm trying to understand what specifically $key => $tmp_name
is doing in the following code:
if(isset($_FILES['files']))
{
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name)
{
echo $_FILES['files']['name'][$key], "
";
move_uploaded_file($tmp_name, 'img/'.$_FILES['files']['name'][$key]);
}
}
This is how you access your values if you have an upload form that has multiple fields, all named files[]
or files[x]
where x
is a number:
example html:
<input type="file" name="files[]" /> // first field
<input type="file" name="files[]" /> // ...
<input type="file" name="files[]" /> // ...
The confusing thing is that the sent-in values are not grouped by index, but by their property (error
, tmp_name
, etc.).
Edit: Based on your edit, I would recommend reading up on foreach
and arrays
in general.