I have an HTML form that contains the following code:
<input id="image_22" name="images[]" type="file" />
<input id="image_8" name="images[]" type="file" />
I’d like to be able to store the form input ID element in a variable (e.g. $imgnumber = ‘image_22’). Is this possible? See my comment in the code below:
PHP:
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
$number_of_file_fields++;
if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
$number_of_uploaded_files++;
$uploaded_files[] = $_FILES['images']['name'][$i];
if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
$number_of_moved_files++;
// *** COMMENT *** //
$imgnumber = CURRENT_FILE_INPUT_ID - this would store e.g. "image_22"
}
}
}
Many thanks for any help with this 🙂
Only the
nameand value of inputs are submitted to the server side. You could code the ID into the name instead of using an array of files.Access on the PHP side:
Edit:
Your updated PHP code could be:
Failing that, you will have to send the ID as a separate variable, for example using a hidden input or the querystring.