neo71 Posted December 5, 2023 Share Posted December 5, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/317508-upload-a-file-to-a-selectable-folder/ Share on other sites More sharing options...
Phi11W Posted December 5, 2023 Share Posted December 5, 2023 How does the User tell your code which "category" a file belongs to? Presumably, that would be another Form field, passed at the same time as the uploaded file itself. [Validate and then] Use that value to construct the target path for the file and pass that value to move_uploaded_file(). // pseudo-code if ( isset( $_POST[ 'btn-upload' ] ) ) { if ( validatePostArguments( $_POST ) ) { $targetFile = buildTargetPath( $_POST[ 'category' ], $_FILES[ 'file' ] ); move_uploaded_file( $file_loc, $targetFile ); } } Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/317508-upload-a-file-to-a-selectable-folder/#findComment-1613321 Share on other sites More sharing options...
neo71 Posted December 6, 2023 Author Share Posted December 6, 2023 Thak u!. users choose a category by a select: <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> $category = $_POST[‘siti’]; Quote Link to comment https://forums.phpfreaks.com/topic/317508-upload-a-file-to-a-selectable-folder/#findComment-1613354 Share on other sites More sharing options...
Phi11W Posted December 6, 2023 Share Posted December 6, 2023 1 hour ago, neo71 said: $category = $_POST[‘siti’]; Or, perhaps better (i.e. safer) ... switch( $_POST[ 'siti' ] ) { case: 'FIUMI' : case: 'MOLINI' : case: 'VITA' : $category = $_POST[ 'siti' ] ; break; default: throw new \Exception( 'Invalid category!' ); } Why? Just because you send the User an HTML SELECT list to use does not guarantee that the value you receive comes from that list! Regards, Phill W. Quote Link to comment https://forums.phpfreaks.com/topic/317508-upload-a-file-to-a-selectable-folder/#findComment-1613355 Share on other sites More sharing options...
Olumide Posted December 7, 2023 Share Posted December 7, 2023 This should help. $category = $_POST['category']; $folder1 = "uploads/categoryA/"; $folder2 = "uploads/categoryB/"; $folder3 = "uploads/categoryC/"; switch ($category) { case 'categoryA': $uploadFolder = $folder1; break; case 'categoryB': $uploadFolder = $folder2; break; case 'categoryC': $uploadFolder = $folder3; break; default: die("Invalid category"); } // it check if the folder exists, if not, it create it if (!file_exists($uploadFolder)) { mkdir($uploadFolder, 0777, true); } $uploadedFile = $_FILES['file']['tmp_name']; $destination = $uploadFolder . $_FILES['file']['name']; if (move_uploaded_file($uploadedFile, $destination)) { echo "File successfully uploaded to $uploadFolder"; } else { echo "Error uploading file"; } Quote Link to comment https://forums.phpfreaks.com/topic/317508-upload-a-file-to-a-selectable-folder/#findComment-1613390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.