Web Analytics Made Easy -
StatCounter Upload Code - Need Small Change - CodingForum

Announcement

Collapse
No announcement yet.

Upload Code - Need Small Change

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Upload Code - Need Small Change

    Hello,

    I have this simple uploader on my website, here is the code:


    The Form:

    <form action="UploadCode.php" method="post" ENCTYPE="multipart/form-data">
    File: <input type="file" name="file" size="30>
    <input type=submit value="Upload">
    </form>



    The Code:

    <?php

    $uploaddir = "Uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
    $allowed_ext = "jpg, gif, png"; // These are the allowed extensions of the files that are uploaded
    $max_size = "50000"; // 50000 is the same as 50kb
    $max_height = "100"; // This is in pixels - Leave this field empty if you don't want to upload images
    $max_width = "100"; // This is in pixels - Leave this field empty if you don't want to upload images
    // Check Entension
    $extension = pathinfo($_FILES['file']['name']);
    $extension = $extension[extension];
    $allowed_paths = explode(", ", $allowed_ext);
    for($i = 0; $i < count($allowed_paths); $i++) {
    if ($allowed_paths[$i] == "$extension") {
    $ok = "1";
    }
    }

    if ($ok == "1") {
    if($_FILES['file']['size'] > $max_size)
    {
    print "
    <b>Error:</b> File size is too big!";
    exit;
    }

    if ($max_width && $max_height) {
    list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
    if($width > $max_width || $height > $max_height)
    {
    print "<b>Error:</b> File height or width are too big!";
    exit;
    }
    }

    if(is_uploaded_file($_FILES['file']['tmp_name']))
    {
    move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
    }
    print "<b>Completed:</b> Your picture has been submitted successfully!";
    } else {
    print "
    <b>Error:</b> Incorrect file extension!";
    }
    ?>




    All I would like to change is that the user who wants to upload a picture can choose the directory to upload the picture to, from a drop down select list in the form.

    So I would like the user to be able to choose the $uploaddir = "Uploads";

    Thanks,
    James

  • #2
    How about something like this. Below I assume your form select field is called 'dropdown'.

    1. Add code at the start where the script assigns the value of the dropdown menu to a variable called $dropdown.

    PHP Code:
        if($_POST['dropdown'] != ''
          {
    $dropdown $_POST['dropdown'];} 
    2. Change the upload directory to the variable.

    PHP Code:
    $uploaddir $dropdown

    Think that should work. You may have to fiddle with the format of the second bit of code (either '$dropdown'; or '.$dropdown.';) to get it to work. I am a beginner also but trying to get to grips with things. Please correct me if I've got something wrong.

    Comment


    • #3
      Thanks, that helped alot.

      Comment

      Working...
      X