Upload a file to a selectable folder

I need to upload a file to a folder based on one of three possible categories

$folder1= “uploads/categoryA/”;
$folder2 = “uploads/categoryB/”;
$folder3 = “uploads/categoryC/”;

with a select I set the categories to the variable

then I upload

if (isset($_POST[‘btn-upload’])) {
if (isset($_FILES[‘file’]))
{ move_uploaded_file($file_loc,$folder1.$final_file); }

this works but I would like the file to be uploaded via $category in the corresponding $folder

What criteria are you basing the directory location on?

by a form

<fieldset>
Sellct category:<br>
<select name="siti" onchange="this.form.nome_mulino.disabled=(this.options[this.selectedIndex].value=='MOLINI')?false:true" >
<option value="MOLINI" selected="selected">ACQUA, FORZA MOTRICE. I MOLINI, RISORSA DI IERI E DI DOMANI</option>
<option value="VITA">LA VITA NEL FIUME</option>
<option value="FIUMI">I NOSTRI FIUMI VISTI DALL’ALTO</option>
		
Nome mulino<input type="text" name="nome_mulino" disabled="disabled" />
</select>
</fieldset>


$categoria = $_POST[‘siti’];

If it’s done by a form value, then change the value of the options to numeric values and the processing to be a dictionary. Then use the value from the dictionary to direct the upload.

I’m sorry but I didn’t understand very well… could you please give me an example?

<fieldset>
		Scegli la categoria:<br>
		<select name="siti" onchange="this.form.nome_mulino.disabled=(this.options[this.selectedIndex].value=='MOLINI')?false:true" >
		<option value="1" selected="selected">CATEGORY A</option>
		<option value="2">CATEGORY B</option>
		<option value="3">CATEGORY C</option>
		
		Nome mulino<input type="text" name="nome_mulino" disabled="disabled" />
		</select>
		</fieldset>

******************************
<td colspan="2"><input name="file" type="file" /><br></td>
**********************************************

And then?

if (isset($_POST['btn-upload'])) {									
if (isset($_FILES['file'])) 
{ move_uploaded_file($file_loc,$folder1.$file); }

Thak you

$fileDir= [
1 => "categoryA",
2 => "categoryB",
3 => "categoryC",
];


if (array_key_exists($_POST['siti'], $fileDir) && isset($_FILES['file']) {
    try {
        move_uploaded_file($fileDir,$folder1.$file);
    } catch( exception $e) {
      // log the exception and inform user
    }

Something like this.

Sponsor our Newsletter | Privacy Policy | Terms of Service