PHP / HTML uploading script not working right
===

I'm pretty new in PHP (don't have much HTML experience, too) and I have a question. I have a file upload system which consists of two files (see below).
In the file upload form an user selects file, selects saving folder and then uploads. Saving location can be chosen from a drop-down menu which consists of folders in uploading directory. But there's another option in the drop-down menu. If the user chooses the option "other" then a new field comes up (it's done by JavaScript) and the user can insert location manually (for example, a directory doesn't exist and the user wants to make a new one). But here's a problem: if the user chooses a folder which is predefined everything is OK, but if the user writes a directory name to the field then only folder called "folder" will be created. For example, if the user wants to upload a picture about a flower and no folder called "flower" exists then the user chooses "other" and writes "flower" into the box, then upload button is clicked and basically PHP should make a new folder called "flower" and upload the picture there but at the moment PHP makes a folder called "folder" and for some reason the picture IS NOT even uploaded there (it's uploaded nowhere). 
My problems with the script:
1) A new folder won't be created. I think it's because I have two values called "folder" but I don't know how to get it work that if "other" is selected then PHP only looks the last value.
2) Even if the folder is created then a file won't be uploaded there for some reason.

I would be really grateful if anyone finds the solution for my problem and helps me to fix it. And don't hesitate to ask for extra information because my English isn't the best and because of this some things can't be understood.

Thanks in advance!


1st file: file-upload.php

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--

    function Toggle(obj){
    var val=obj.value;
    if (!obj.m){ obj.m=''; }
    if (!obj.m.match(val)){ obj.m+=','+val+','; }
    var hide=obj.m.split(',');
    for (var zxc0=0;zxc0<hide.length;zxc0++){
    if (document.getElementById(hide[zxc0])){
    document.getElementById(hide[zxc0]).style.display='none';
    }
    }
    var show=val.split(',');
    for (var zxc1=0;zxc1<show.length;zxc1++){
    if (document.getElementById(show[zxc1])){
    document.getElementById(show[zxc1]).style.display='';
    }
    }
    }

    //-->
    </script>
    </head>
    <body>
    <h1>Upload</h1>
    <form action="upload.php" method="post"
    enctype="multipart/form-data"><br>
    <label for="file">File:</label>
    <input type="file" name="file" id="file" /> 
    	<br>
    	Tüüp: <select name="folder" value="folder" onchange="Toggle(this);">
    <?php  
    foreach(glob('/home/cemica/domains/mihkel.era.ee/public_html/koduleht/upload/*',      GLOB_ONLYDIR) as $dir) 
    { 
        $dir = basename($dir); 
        echo '<option value="', $dir, '">', $dir, '</option>'; 
    }  
    ?>
    <option value="folder">other</option>
            </select><br> 
      
    <input id="folder" value="Create a new folder" style="display:none;">
    <input type="submit" name="submit" value="Upload" />
    </form>
    </body>
    </html>

2nd file: upload.php

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title></title>
    </head>
    <body>
    <h1>Upload</h1>
    <br>

    <?php
    $dirname = $_POST["folder"];
    $filename = ("/uploadfolder/" . "$dirname" . "/");
    
    if (!file_exists($filename)) {
        mkdir("/uploadfolder/" . "$dirname", 0777);
        exit;
    } else {
    }
    ?>

    <?php

    $folder = $_POST["folder"];

    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-     officedocument.spreadsheetml.sheet"))
    && ($_FILES["file"]["size"] < 5120000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Name: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Tmp. file: " . $_FILES["file"]["tmp_name"] . "<br />";
        echo "Folder: " . $folder . "<br />";
    
        if (file_exists("/uploadfolder/$folder/" . $_FILES["file"]["name"]))
          {
           echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "/uploadfolder/$folder/" . $_FILES["file"]["name"]);
          echo "Saved: " . "" . $_FILES["file"]["name"];
          }
         }
      }
    else
      {
      echo "Upload failed";
      }
     ?>
    </body>
    </html>