• Simple PHP upload form + Radio button code help

    Miranda65M Member

    I need simple html+php form for my requirement. I want simple file uploader with radio button

    Here is code


    Choose a file to upload:


    $target_path = "uploads/";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name']).
        " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";

    now radio button code
    The PHP code:


    The HTML FORM code:

    >Male >Female

    As a newbie in php it is difficult to me to combine in one 
  • ShikhaTan Member

    I fixed your script you can upload now images to the directory “uploads”.
    Here is the code:

       Simple Upload Form
    Choose a file to upload:
    >Male >Female
  • Abhey Member

    You should handle these things with only one form. Also, some error reporting is also good, so I added that for you too.

    You should also add some validation of user data somewhere in there, but I will let you think about it first.

    Also, proper indenting is very important for more complex codes and I suggest you to get on with it right when you start programming (if you are looking for a future in this profession, I mean).

    Examine the code and if you have questions, feel free to ask.

          Simple Upload Form
           'The uploaded file exceeds the upload_max_filesize directive in php.ini',
             2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
             3 => 'The uploaded file was only partially uploaded',
             4 => 'No file was uploaded',
             5 => 'Missing a temporary folder',
             6 => 'Failed to write file to disk',
             7 => 'A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help',
          $radio_info = array(
             'male' => 'You think you are a male.',
             'female' => 'You think you are a female.',
          function get_document_root() {
              $server = $_SERVER["DOCUMENT_ROOT"];
              $last = substr($server, -1);
              $slash = $last == '/' ? '' : '/';
              return $_SERVER["DOCUMENT_ROOT"] . $slash;
          if (isset($_POST['Submit'])) {
             // handle gender select
             $selected_radio = $_POST['gender'];
             if ($selected_radio == 'male') {
                $male_status = 'checked = "checked"';
             if ($selected_radio == 'female') {
                $female_status = 'checked = "checked"';
             echo $radio_info[$selected_radio] . '
    '; // handle file upload $root = get_document_root(); $plus_path = "YOUR_UPLOAD_FOLDER/uploads/"; // edit this to your path $target_path = $root . $plus_path . basename($_FILES['uploadedfile']['name']); if ($_FILES['uploadedfile']['error'] > 0) { echo $file_errors[$_FILES['uploadedfile']['error']]; } else { if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename($_FILES['uploadedfile']['name']) . " has been uploaded."; } else { echo "There was an error uploading the file, please try again!"; } } } ?>
    Choose a file to upload:
    />Male />Female
Viewing 2 reply threads
  • You must be logged in to reply to this topic.