It's definitely doable, and probably isn't overly difficult so it should make a good little project for you to learn php.
- Split the string $file_name[$i] and grab everything after the period (the extension)
- Create a big if..then...elfseif block and compare that extension name to the types of files you're allowing to be uploaded (txt, jpg, etc.)
- In each branch of the if statement, take the variable called $folder, and set it equal to the appropriate folder name (txt,jpg,etc.)
That should be all you need to do, barring any surprises.
Jam it back in, in the dark.