Do you know how to upload image in database using PHP with form?

Upload image in database using PHP


The first thing is that we can only store the name of an image in database and image upload in our project folder.

Code is here -: 

  • ImageForm.php

    • <!DOCTYPE html>
    • <html>
    • <head>
    • <title>image</title>
    • </head>
    • <body>
    • <form action="image.php" method="post" enctype="multipart/form-data">
    • <input type="file" name="img">
    • <input type="submit" name="sub" value="submit">
    • </form>
    • </body>
    • </html>

  • image.php

    • <?php

    • $image_name = $_FILES['img']['name']; //image name
    • $tmp_name = $_FILES['img']['tmp_name']; //temporary name

    • move_uploaded_file($tmp_name, "images/$image_name");

    • $hostname = 'localhost';
    • $username = 'root';
    • $password = '';
    • $databasename = 'testing';

    • $u_name = 'hello908';

    • $master = mysqli_connect($hostname, $username, $password, $databasename); //database conneciton

    • if($master) {
    • echo "connection is established!..";
    • //insert data
    • $query = "INSERT INTO `image upload`(`image_name`) VALUES ('$image_name')"; //insert
    • //execute
    • $run = mysqli_query($master, $query);
    • if($run) {
    • echo "<br> image uploaded!..";
    • }
    • else{
    • echo "<br> image not uploaded!..";
    • }

    • }
    • else {
    • echo "connection is not established!..";
    • }


    • ?>
Table link -: https://drive.google.com/file/d/1iNDI8i2ODtZ_bpGuYsJlqv-MsPkmQGJ3/view?usp=sharing



No comments