How to Upload Image in PHP MySQL

By Super Admin | Oct 16, 2021 | PHP
Share :

https://www.fundaofwebit.com/post/how-to-upload-image-in-php-mysql

Upload Image in PHP MySQL


In this post, you will be learning about how to upload Image in php mysql, where the image will be uploaded in an folder and store its image name in database by renaming the image. | Upload image in php mysql database

So, Lets get started with an example of User Profile Image.

Step 1: Create a table named users

CREATE TABLE users (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    fullname VARCHAR(30) NOT NULL,
    image VARCHAR(50)
)

Step 2: Let's create a form to upload the Image in a file named index.php 

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload Image in PHP MySQL</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
   
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card shadow">

  <?php
                        if(isset($_SESSION['message']))
                        {
                            echo '<h4 class="alert alert-warning">'.$_SESSION['message'].'</h4>';
                            unset($_SESSION['message']);
                        }
                    ?>

                    <div class="card-header">
                        <h4>Upload Image in PHP MySQL</h4>
                    </div>
                    <div class="card-body">

                        <form action="code.php" method="POST" enctype="multipart/form-data">

                            <div class="mb-3">
                                <label>Full Name</label>
                                <input type="text" name="fullname" class="form-control">
                            </div>
                            <div class="mb-3">
                                <label>Upload Image</label>
                                <input type="file" name="image_profile" class="form-control">
                            </div>
                            <div class="mb-3">
                                <hr/>
                                <button type="submit" name="upload_save_btn" class="btn btn-primary">Upload</button>
                            </div>

                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

Step 3: Create a folder named uploads in your project to store the image.
Now, on form submit, your data is posted to code.php. So guys, lets create a code.php file and paste the below code:

<?php
session_start();
$con = mysqli_connect('localhost','root','','database_name');

if(isset($_POST['upload_save_btn']))
{
    $fullname = mysqli_real_escape_string($con, $_POST['fullname']);
    $image = $_FILES['image_profile']['name'];

    $path = "uploads/"; /** Path for Uploading your Image **/
       
    $image_extension = pathinfo($image, PATHINFO_EXTENSION); /** Image Extension **/
    $filename = time().'.'.$image_extension; /** Renaming the Image **/

    $query = "INSERT INTO users (fullname,image) VALUES ('$name','$filename')";
    $query_run = mysqli_query($con, $query);

    if($query_run)
    {
        // Upload Image to uploads folder
        move_uploaded_file($_FILES['image']['tmp_name'], $path."/".$filename);

        $_SESSION['message'] = "Image User Profile Uploaded Successfully";
        header('Location: index.php');
        exit(0);
    }
    else
    {
        $_SESSION['message'] = "Something went wrong";
        header('Location: index.php');
        exit(0);
    }
}
?>

That's it. Now you can see your uploaded Image in your created folder. i.e uploads/


Thanks for reading..

https://www.fundaofwebit.com/post/how-to-upload-image-in-php-mysql

Share this blog on social platforms