Upload Image in PHP MySQL Database Example

Mar 03, 2022 . Admin

Hello Friends,

I am going to explain you upload image in PHP MySQL Database example. You will learn how to upload image in db using PHP. In side this article we will see how to upload images in PHP with Database.

This article will give you simple example of PHP MySQL upload image. We will use get simple PHP code for upload image in Database.

You can use from how to upload image in Database using PHP and MySQL. I will give you simple how to PHP upload photo to Database.

So, let's see bellow solution:

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Upload Image in PHP MySQL Database Example - Mywebtuts.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

        $conn = mysqli_connect('localhost', 'root', '', 'test');
            die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);    


            if(preg_match("/\S+/", $_FILES['image']['name']) === 0){
                $errors['image'] = "*The image field is required.";

            $filename = $_FILES['image']['name'];
            $imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
            $extensions_arr = array("jpg","jpeg","png");
            if(in_array($imageFileType,$extensions_arr) ){
                    $insert = "INSERT INTO images(image_name) values('$filename')";
                    if(mysqli_query($conn, $insert)){
                        $successful = '<h5 class="alert alert-success">Image store successfully!</h5>';
                        echo 'Error: '.mysqli_error($conn);
                    echo 'Error in uploading file - '.$_FILES['image']['name'].'<br/>';

    <div class="container mt-5">
        <div class="row">
            <div class="col-md-12">
                <div class="card m-auto w-75">
                    <div class="card-header text-center bg-success text-white">
                        <h3 style="font-size: 25px;">Upload Image in PHP MySQL Database Example - Mywebtuts.com</h3>
                    <div class="card-body pb-1">
                        <form method='POST' action='#' enctype='multipart/form-data'>

                            <?php if(isset($successful)){ echo $successful; } ?>

                            <div class="form-group ">
                                <div class="custom-file">
                                    <input type="file" name="image" id="customFile" class="custom-file-input">
                                    <label class="custom-file-label" for="customFile">Choose file</label>

                                    <?php if(isset($errors['image'])){echo "<span class='text-danger'>" .$errors['image']. "</span>"; } ?>

                           <div class="form-group d-flex justify-content-center"> 
                               <input type='submit' name='submit' value='Upload' class="btn btn-primary px-4">

        $(".custom-file-input").on("change", function() {
            var fileName = $(this).val().split("\\").pop();

It will help you...