Registration form - upload image

32,239

In code

Before

<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
    <input type="file" id="inputImage" Name="photo">
    <button class="btn btn-large btn-success" type="submit">Register</button>
</form>

After changes

<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post" enctype="multipart/form-data">
    <input type="file" id="inputImage" Name="photo">
    <button class="btn btn-large btn-success" type="submit">Register</button>
</form>

You just add enctype="multipart/form-data" in your html form tag

Description

1.When you upload any types of files from form you must write enctype attribute in form element.

Thank you

Share:
32,239
user2462645
Author by

user2462645

Updated on January 11, 2020

Comments

  • user2462645
    user2462645 over 4 years

    I'm working on a registration form for a website, I'm trying to upload a photo to the server and pass the file path on to the database.

    This is probably just a simple issue, as i'm quite the beginner to php and mysql.

    Here's my standard form, which we'll call register.php. I cut out all other input than the image.

    <form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
        <input type="file" id="inputImage" Name="photo">
        <button class="btn btn-large btn-success" type="submit">Register</button>
    </form>
    

    This is the execution file, which we'll call code_exec.php

    <?php
    session_start();
    include('connection.php');
    
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $mname = $_POST['mname'];
    $address = $_POST['address'];
    $contact = $_POST['contact'];
    $pic = $_POST['pic'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $skype = $_POST['skype'];
    $email = $_POST['email'];
    
    //This is the directory where images will be saved 
    $target = "upload/"; 
    $target = $target . basename( $_FILES['photo']['name']); 
    
    //This gets all the other information from the form 
    $pic = ($_FILES['photo']['name']); 
    
    //Writes the photo to the server 
    if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { 
        //Tells you if its all ok 
        echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
    } else { 
        //Gives an error if its not 
        echo "Sorry, there was a problem uploading your file.";
    } 
    
    mysql_query("INSERT INTO member(fname, lname, gender, address, contact, picture, username, password, skype, email, photo)VALUES('$fname', '$lname', '$mname', '$address', '$contact', '$pic', '$username', '$password', '$skype', '$email', '$pic')");
    
    header("location: index.php?remarks=success");
    mysql_close($con);
    
    ?>
    

    How do I find the path of the image file?