Creating dynamic file input elements based on the selected element from the dropdown

Hello All,
I am trying to create dynamic file upload elements based on the selected value of the multiple drop-down and once the form is submitted, it should save the file as the same ID of the selected value.
What i have done so far and works fine

  1. Select 2 Multi-select drop-down box- get values from db .
    2)JavaScript creates dynamic file input on change of the selected value of the multi-select.
  2. On file upload java script checks for file validation
    after the form is submitted
  3. PHP validates inputs and file upload
  4. Inserts all the multi-selected value in the DB
    The problem area
    Doesn’t upload the file and replace the file name with the selected id
    The problem area is when more than one value is selected in the drop-down it inserts the value fine but uploads the same file name for both the selected values in the drop down even if there is no file uploaded for the first selected value in the drop-down.
    I am only providing php code after the post
if ( ( $_SERVER[ "REQUEST_METHOD" ] == "POST" )){
	
	$file_name = isset( $_FILES['files']['name'] ) ? $_FILES['files']['name'] : "";
	$multidrp= isset( $_POST[ "multidrp" ] ) ? $_POST[ "multidrp" ] : "";
       if ( !empty( $multidrp) ) {
			foreach ($multidrp as $multidrpK => $multidrpv ) {
				echo $multidrp[$multidrp]."<br>";
                  if(!empty($file_name)){
				$upltype = "type1";
				echo "I will upload type1document";
				**UploadFile($upltype,$multidrp[$multidrp],$file_name);**
			}
	}
}
	 else {
			echo "multiselect is empty";
		}
	exit;
}


The upload file function just saves the file.

I hope i have provided all the info needed.

I would really appreciate any suggestion on what i am doing wrong.

Many Thanks,
Teju

Are you sure that is right?

Sorry, I have updated it to use $multidrp[$multidrpK] but still no luck. Thank you for spotting it.

Could some one please shed any light on this. I am driving myself nuts. Probably something really silly but I am not able to spot it.

Many Thanks,
Teju

It might help if you showed us your UploadFile function.

function UploadFile($upltype,$multiselectid,$fname ) {

	
	$max_file_size = 10485760; #(1024*1024*10 in MB)
	$path = "/fppath/";
	
if ( $upltype == "type1" ) {	
	  
	  echo "Type:type1";
	  $errors= array();
      $file_name = $_FILES['files']['name'];
      $file_size =$_FILES['files']['size'];
      $file_tmp =$_FILES['files']['tmp_name'];
      $file_type=$_FILES['files']['type'];
         
      $extensions= array( "docx", "doc", "pdf" );
    
	$ext = explode( ".", $file_name );
	$ext = end( $ext );
     if(in_array($ext,$extensions)=== false){
        $errors[]="extension not allowed.";
    }
      
      if($file_size > $max_file_size){
         $errors[]='File size';
      }
      
     if(empty($errors)==true){
		  echo "No errors";
      $file_name = preg_replace( '/\s+/', '_', $file_name );
	  $userfname = $file_name;
	  print_r($userfname);
		 //if(move_uploaded_file($_FILES["files"]["tmp_name"], $path.$userfname))
	  echo "Success";
	} 
		else{
         print_r($errors);
         
      }
      }
	
}

Here is the file upload function for your reference.

The line that actually copies the file is commented out.

I do realise that, as per the question the problem area is when more than one value is selected in the drop-down it inserts the value fine but uploads the same file name for both the selected values in the drop down even if there is no file uploaded for the first selected value in the drop-down.

I think i have somewhere looping issue but not sure where .

And I don’t see anywhere in that function that even looks at the value of the parameter $multiselectid, never mind actually does anything with it.

Why do you pass the second and third parameters to the function, then not use them at all?

You are right, all i am doing is the passing the file name and not using it. Its left over thing from my trial and error code.

My problem is ,if i pass the entire file object and then access the properties from the passed file contents rather than again accessing the file within the function I cannot get all the other properties of file like size name etc.

I am not sure what is the right way to do this.

I’m not really all that clear on what you are doing. If you have multiple items selected in “multidrp”, do you want to upload the same file for each one? Or a different file?

If you’re just going to use $_FILES from inside your function, there’s no real point having the function at all, you just might as well leave the code in line.

I want a different file uploaded depending on the user input. So for example

The user selects 3 items(can select more or less) from the multidropdown list. The java-script generates 3 different file input elements(with input id as the multidropdown id) for this 3 selected items in the div area exactly below the drop down list .

The user might just wish to upload a file for the first selected item or all the three selected items or none at all.

My problem is even if the user just uploads file only for first element and the rest two are blank . The code still uploads the same file for all the three elements completely ignoring that they are different input elements.

I have a feeling that there is a problem while looping this elements.
I hope i have tried to explain you what i want to achieve out of this.

Many Thanks,
Teju

As I read it, the code doesn’t look at the multidropdown id to decide which file it’s going to upload. You do loop through it, but even though you pass a reference to $multidrp to the function, you’re not using it, just using $_FILES['files'] for everything.

You could pass through the name attribute to your uploadFile() function and use that in the $_FILES array, or you could pass through the whole array.

What name attribute does it assign to each file element? That would be the way you’d access it in the $_FILES array as far as I know.

This has worked wonders. I was creating the element with the same name but different ids. I have aded a javascript to change the name of the attribute to the id.

Below is what i have done in php

if ( ( $_SERVER[ "REQUEST_METHOD" ] == "POST" )){
	
	
	$multidrp= isset( $_POST[ "multidrp" ] ) ? $_POST[ "multidrp" ] : "";
       if ( !empty( $multidrp) ) {
			foreach ($multidrp as $multidrpK => $multidrpv ) {
				echo $multidrp[$multidrpK]."<br>";
$multiid=$multidrp[$multidrpK];
                               $inputfiles = $_FILES['files'.$multiid];
				if (!empty($inputfiles ['name']))
				{
				$upltype = "type1";
				echo "I will upload type1document";
				**UploadFile($upltype,$multidrp[$multidrpK],$inputfiles );**
			
	}
}
	 else {
			echo "multiselect is empty";
		}
	exit;
}

Thank you so much . The solution was so simple
Many Thanks,
Teju

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.