Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8912359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:10:44+00:00 2026-06-15T04:10:44+00:00

I am using the following HTML & PHP code to move multiple images to

  • 0

I am using the following HTML & PHP code to move multiple images to my server.

Is there a way using my code that I can associate a specific input element with the uploaded file?

So in my for loop, I can discover when the image from ‘image_22‘ is being processed. Is this possible using my current code?

Dream world would be storing the value “image_22” inside a variable $imageNum 🙂

 

Here is a snippet of what I’m currently working with…

HTML:

<input id="image_22" name="images[]" type="file"  />
<input id="image_8" name="images[]" type="file"  />  
... 

PHP:

<?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 procee 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++;
            }

        }

    }
    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);
    }
    ?> 

Thank you!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:10:46+00:00Added an answer on June 15, 2026 at 4:10 am

    In your HTML, just include that value inside the [].

    <input id="image_22" name="images[22]" type="file"  />
    <input id="image_8" name="images[8]" type="file"  />  
    

    In your loop, instead of an incremental for loop, use a foreach with index, which is a more common pattern in PHP than the incremental type. The $index will be the number supplied in the []:

    // Loop over the ['name'] key in $_FILES['images'] to get all the named indexes
    foreach ($_FILES['images']['name'] as $index => $filename) {
    
      if ($filename != '') {
         // not empty...
         $number_of_uploaded_files++;
    
         // Check for validity (see below)...
    
         // Use the name concatenated with _$index to supply store the index with the filename
         $uploaded_files[] = $filename . "_$index";
         if (move_uploaded_file($_FILES['images']['tmp_name'][$index], $upload_directory . $filename . "_$index")) {
           // successful rename
           $number_of_moved_files++;
         }
      }
    }
    

    Note that your script is currently vulnerable to path injection attacks. You must filter the name of each file against the inclusion of things like ../ which could force the file to be saved anywhere on your filesystem (writable by the web server)! It is recommended to check the name with a regular expression of acceptable values:

    // Verify that the uploaded filename contains only letters, numbers, hyphen, underscore, space before the `.` and letters only after the `.`
    // You could also insist that it end in `.(jpg|gif|png)` or whatever your acceptable formats are
    // Most important is to prevent things like `../`
    if (preg_match('/^[a-z0-9_- ]+\.[a-z]+$/i', $filename)) {
      // It's an ok filename
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the following code in html to call a php file to
Can anyone provide either a method of using the following code that doesn't result
I am using the following code: $("#treeview").jstree(); $("#treeview").jstree('open_all'); With the following html: <div id="treeview">
I'm trying to create css buttons by using the following html markup: <a href=access.php
I'm trying to generate the following html code using cl-who: <html> <body> <div id=cnt_1></div>
I open a pop-up window using following code in main.html function openwindow(url) { window.open(url,
I tried using the following code for a HTML page, but it doesn't work.
I am using the following code to fetch html source website. private string Extract_Source(string
I am using the following code in WINDOWS OS and PHP script, in which
I was using the following code to retrieve HTML snippets from a database table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.