PHP 8 Image Upload in MySQL Database Example




PHP 8 Image Upload in MySQL Database Example

Hello Friends,

In this example, I will show you PHP 8 Image Upload in MySQL Database Example. I explained simply about How to Upload Image in Database using PHP 8?.I will give you simple example of How to upload images into MySQL database using PHP 8 ? .it's simple example of How to insert image in MySQL database using PHP 8?. So, let's follow few step to create example of How To Upload And Insert Image Into Mysql Database Using Php And Html.

Here you will learn Upload and Store Image File in Database using PHP 8 and MySQL.

So, let's see bellow solution:




<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>PHP 8 Image Upload in MySQL Database Example -</title>

<link rel="stylesheet" href="">

<script src=""></script>

<script src=""></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;">PHP 8 Image Upload in MySQL Database Example -</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...

#PHP 8