PHP Uploading dyanmic file Upload

Hello,

I am trying to create a dynamic file upload for all the items selected in the multi select drop down. I have managed to create dynamic div along with the number of file input elements depending on the selected values. I am having trouble while saving in the database identifying which file is for which selected value . I am looking to somehow identify the id and insert in the database say for e.g below is the selected values from the multi select drop down

id1 text1 file1
id2 text2 file2

The trouble that i am having is while inserting in the database identifying whats the id of file.

I would really appreciate any help on this as just running out of ideas on how best to achieve this.
Many Thanks,
Teju

Could you use arrays for the various form inputs, and then it’s just a case of grabbing the array index and using it for id, text and file.?

Hiya,
At the moment i am looping through the all the selected inputs of the drop down and looping the file uploads .
I am getting the ids fine for the drop down but its a problem for file upload.

I just am not getting the selected id and the filename that was associated for the selected value from the drop down list.

Many Thanks,
Teju

Hard to say any more without seeing the relevant bits of code.

Here is what i am using in the php code to get the values and the files. I am sure i am missing something but can’t figure out what.

$multiselect=isset($_POST["$multiselect"]) ? $_POST["$multiselect"] : "";
		//santise inputs
			if (!empty($multiselect))
				 {
				foreach ($multiselect as $multiselectK=> $multiselectv){
					$sqlINSmult = "INSERT INTO xxxxxxx(x,x) VALUES ($testid,'$multiselect[$multiselectK]')";
					$connection->exec($sqlINSmult);	
		    	//Checkif there was any file uploaded for the multiselect values
				
					 if (!empty($file[$multiselectK])){
						
						$valid_formats = array("img", "jpg", "png");
						$max_file_size = 10485760; #(1024*1024*10 in MB)
						$path = "xxx";  
						$count = 0; 	
					
						if ($_FILES['file']['error'] == 4) {
							//echo "There is an error";
							//die; // Skip file if any error found
						}	       
						if ($_FILES['file']['error'] == 0) {	           
							if ($_FILES['file']['size'] > $max_file_size) {
								$message = "$name is too large!.";

								echo '<div class="alert alert-danger">'.$message.'</div>'; 

							}
							elseif(!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
								$message = "$name is not a valid format";
								echo '<div class="alert alert-danger">'.$message.'</div>';
								die;

							}
							else{ // No error found! Move uploaded files
							//rename the file
								$ext = explode(".", $name); 
								$ext=end($ext);
								// Explode file name from dot(.)
								$name = preg_replace('/\s+/', '_', $name);
								$userfname=$name;
								$fname="ff-".$multiselect[$multiselectK].".".$ext ;
								echo $fname;
								//if all ok insert the filenames and multiselect ids in the db
								
							}
						}
					
					 }
				}

				}

Many Thanks for your help.

I admit my morning dose of caffeine hasn’t kicked in yet, but I’m not seeing where the $file[$multiselectK] array is coming from.

I was presuming that the $multiselect variable in the first line is coming from a loop of some sort, not that the HTML code actually has field names starting with a $ symbol. $multiselectK is then the key provided by the foreach() loop.

It would possibly be handy to see the form html code and the bit of PHP before this one, in case there’s a better way to loop through the form fields.

I think i have figured it out. Although, i will need to test it thoroughly but seems to be getting ID and file name correctly.Thank you everyone for your help

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