/    Sign up×
Community /Pin to ProfileBookmark

Storing form field id in a variable during file upload

Hello!

I have a basic script that allows me to upload multiple files to my server – yay!

Now, I’d like to be able to associate a variable with the input element that holds that particular image.

Dream world would be being able to store [B]$imgnumber = “9”;[/B] in my loop if I upload an image to the image_9 field.

[B]HTML Code:[/B]

[code=html]<input id=”image_9″ name=”images[]” type=”file” />
<input id=”image_11″ name=”images[]” type=”file” />[/code]

[B]PHP Code:[/B]

[code=php]
if (isset($_POST[‘Submit’])) {
$number_of_file_fields = 0;
$number_of_uploaded_files = 0;
$number_of_moved_files = 0;
$uploaded_files = array();
$upload_directory = dirname(__file__) . ‘/uploaded/’; //set upload directory
/**
* we get a $_FILES[‘images’] array ,
* we process this array while iterating with simple for loop
* you can check this array by print_r($_FILES[‘images’]);
*/
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++;
// $varnumber = FORM_ELEMENT_ID
}

}

}
echo “Number of File fields created $number_of_file_fields.<br/> “;
echo “Number of files submitted $number_of_uploaded_files . <br/>”;
echo “Number of successfully moved files $number_of_moved_files . <br/>”;
echo “File Names are <br/>” . implode(‘,’, $uploaded_files);
} [/code]

Many thanks for any pointers here 😀

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @invision spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...