phpmyadminblobimage-uploadingformshttp-post

How to upload picture into a database with a html form which have a type = "file"?


I don't really know how to store my pictures in a database. The user will select a file, upload it, and cick to submit button. After it the picture should autimatically be loaded up into the database. Any trivial way to solve it? Should i use BLOB? It'd be more efficient if i could load up the picture into the server, and the sql variable would get the path of the picture. Is this implementable?

              <meta charset="UTF-8"/>
              <form method = "post" id= "myform" name="myform" action = "<?  php $_PHP_SELF ?>">
              <table width = "400" border = "0" cellspacing = "1" 
                 cellpadding = "2">


                    <td><input name = "user_name" type = "text" 
                       id = "user_name"></td>

                  <input name = "add" type = "submit" id = "add" 
                          value = "Küldés">
                 </tr>
               </form>



                    <td><form action="uploadpic.php" method="POST" enctype="multipart/form-data" />
                <input type="file" name="user_image" id="user_image" />

                </form>

Solution

  • Basically, you just use:

    HTML:

    <form action="uploadpic.php" method="POST" enctype="multipart/form-data" />
    <input type="file" name="theimage" />
    <input type="submit" value="Send">
    </form>
    

    PHP: (uploadpic.php)

    <?php
    $uid = uniqid();
    
    $image = $uid . $_FILES['theimage']['name'];
    
    //Make sure that the folder "images/" exists
    move_uploaded_file($_FILES['theimage']['tmp_name'],'images/' . $uid . $_FILES['theimage']['name']);
    
    // Assuming you already have the row and you use a SESSION which stores your username
    $sql = mysql_query("UPDATE your_table SET image = '$image' WHERE username = $_SESSION['username']", $sqlconnection);
    ?>
    

    If there's any question or errors, feel free to comment and I'll help you.