SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 the image.

    Dream world would be storing $imgnumber = "9"; in my loop if I upload an image to the image_9 field.

    Here is a sample of the HTML code:
    HTML Code:
    <input id="image_9" name="images[]" type="file"  />	
    	<input id="image_11" name="images[]" type="file"  />
    Here is a sample of the PHP code:
    PHP Code:
    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);

    Many thanks for any pointers here :-D

  2. #2
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You can specify the key in your form fields and access that key's index# directly in PHP.
    HTML Code:
    <input id="image_9" name="images[9]" type="file"  />
    <input id="image_11" name="images[11]" type="file"  />
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    1,285
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi kduv,

    Thanks for the reply.

    Can you explain how I retrieve the key's index in my for loop example above?

  4. #4
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Use a foreach loop instead of a for loop. It was designed specifically for iterating through arrays and objects.

    In this example, you can access the key VIA the $key variable. I add each non-empty key to an array just for reference, but you can do whatever you want with it. Using something like this you can even name your form fields with named keys instead of just numbers... IE: name="images[thisimage]"

    PHP Code:
    if (isset($_POST['Submit'])) {

        
    $non_empty_keys = array();
        
    $number_of_file_fields c;
        
    $number_of_uploaded_files 0;
        
    $number_of_moved_files 0;
        
    $uploaded_files = array();
        
    $upload_directory dirname(__file__) . '/uploaded/';

        foreach (
    $_FILES['images']['name'] as $key => $val) {
            
    $number_of_file_fields++;
            if (
    $val != '') {//check if file field empty or not

                
    $non_empty_keys[] = $key;
                
    $number_of_uploaded_files++;
                
    $uploaded_files[] = $_FILES['images']['name'][$key];

                if (
    move_uploaded_file($_FILES['images']['tmp_name'][$key], $upload_directory $_FILES['images']['name'][$key])) {
                    
    $number_of_moved_files++;
                }
            }
        }

        echo 
    "These are the keys for non-empty fields: "implode(', '$non_empty_keys), '<br>';
        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);

    Keith
    Freelance web developer
    http://www.duvalltech.com/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •