Select directory from a dropdown and upload file

Tko_1 0 Tallied Votes 3K Views Share

I needed a script that would grab all the folders in the directory and add them to a dropdown list and allow the user to upload to there choosen folder. This is what i came up with. (upload script is not mine) Thought i would share.

    if ($_POST['variable'] == '')
    $variable = './'; // default folder
    $variable = $_POST['variable'] ;
    $folder = $variable;
    $uploadpath = "$folder/";      
    $max_size = 2000;          
    $alwidth = 900;            
    $alheight = 800;           
    $allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');        
    if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
      $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       
      $sepext = explode('.', strtolower($_FILES['fileup']['name']));
      $type = end($sepext);       
      list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     
      $err = '';        
      if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';
      if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';
      if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;
      if($err == '') {
        if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) { 
          echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';
          echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';
          echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';
          if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;
          echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b>';
        else echo '<b>Unable to upload the file.</b>';
      else echo $err;
    <div style="margin:1em auto; width:333px; text-align:center;">
     <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data"> 
      Upload File: <input type="file" name="fileup" /><br/>
    <select name="variable" />
    <option value="" selected="selected">Select a folder</option>
    <form name="input" action="upload.php" method="post" onchange="this.form.submit()">
    $dirs = glob("*", GLOB_ONLYDIR);
    foreach($dirs as $val){
    echo '<option value="'.$val.'">'.$val."</option>\n";
      <input type="submit" name='submit' value="Upload" /> 
Member Avatar

(upload script is not mine)

If the script isn't yours, it might have been easier to provide a link to it - or at least provide an attribution.

Tko_1 0 Junior Poster

lol thought i added that when i editted it, well the upload is from here but i added the dropdown with all the folders. Because i counld find a script that would add folders automatically to a dropdown

Member Avatar

Good job, now the author won't be too miffed :)

kaakka 0 Newbie Poster

i love this scsript but option need file rename and support file txt,php,html

Sadique_1 0 Newbie Poster

what will be thcode for uploading multiple files

increase 0 Newbie Poster

This code is perfect for my needs but it does not allow mp3 or wav auduio how can I modify it so that it will accept these file types?

rproffitt 2,331 "Nothing to see here." Moderator

@increase. This thread has issues.

  1. "(upload script is not mine)" this means that you may want to find the author to ask questions.
  2. This thread is 7 years old. Participants may not reply.
  3. Current members seem to ignore old posts like this.
  4. If you can't modify the code above, you may not be able to maintain it later or even get it working. In this case I suggest you add more members to your team that maintains your websites.
increase commented: THanks but the script works perfectly for images but will not allow audio +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.