We have to add attribute enctype="multipart/form-data" in form tag for uploading image.
<?php include("upload.php"); ?>
<?php include("common/header.php"); ?>
<body>
<div class="container-fluid" style="margin-top: 50px;">
<?php include("common/navbar.php"); ?>
<div class="container w-50">
<div class="text-center"> <?php echo $success; ?> <?php echo $error; ?> </div>
<form method="POST" action="index.php" enctype="multipart/form-data" class="border p-4" style="background:#f5f6fa;">
<h3 class="rounded bg-primary w-100 p-2 text-white text-center">Upload Image</h3>
<div class="form-group">
<label for="my-image">Upload image</label>
<input type="file" class="form-control" name="my-image" id="my-image" style="height: 50px; color:orange; border:2px solid orange">
</div>
<div class="form-group pb-2">
<input type="submit" name="upload" value="Upload Image" class="btn btn-success">
<a href="index.php" class="btn btn-danger float-right">Close</a>
</div>
<form>
</div>
</div>
</div>
</body>
<?php include("common/footer.php"); ?>
You can upload image by click on Upload Image green Button.
CREATE TABLE `images` (
`id` int(11) NOT NULL,
`pic_name` varchar(255) NOT NULL,
`creatde_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
$dbhost ="localhost";
$dbuser ="root";
$dbpswd ="";
$dbname ="dbdemo";
$dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
if($dbcon){
//echo "Connected............!!!";
return $dbcon;
exit();
}else{
die("Connection failed ? ? ?". mysqli_error($dbcon));
}
mysqli_close($dbcon);
PHP code to upload image in folder and in database.
<?php include("dbcon.php");
$error=''; $success='';
if(isset($_POST['upload'])){
$image = $_FILES['my-image']['name']; //['my-image'] from input field attribute name="my-image" & ['name'] index from $_FILES array
$image_temp = $_FILES['my-image']['tmp_name'];//['my-image'] from input field attribute name="my-image" & ['temp_name'] index from $_FILES array
$imageSize = $_FILES['my-image']['size']; //['my-image'] from input field attribute name="my-image" & ['size'] index from $_FILES array
$imageError = $_FILES['my-image']['error']; //['my-image'] from input field name attribute name="my-image" & ['error'] index from $_FILES array
$imageType = $_FILES['my-image']['type']; //['my-image'] from input field name attribute name="my-image" & ['type'] index from $_FILES array
//FIND THE EXTENSION OF FILE LIKE jpg png OR pdf
$fileExtension = pathinfo($image,PATHINFO_EXTENSION);
// Allowed file type extension to upload
$allowedExtension = ['jpg','jpeg','png'];
if($image==""){
$error= "<div class='alert alert-danger text-danger'>Please select image </div>";
}elseif(!in_array($fileExtension, $allowedExtension)){
$error= "<div class='alert alert-danger text-danger'>This type of image not allowed </div>";
}elseif($imageSize>1000000){
$error= "<div class='alert alert-danger text-danger'>The image size can not be bigger than 1 MB </div>";
}else{
$sql = "INSERT INTO images(`pic_name`) VALUES('$image')";
$run = mysqli_query($dbcon,$sql);
move_uploaded_file($image_temp, "upload/$image");
if($run){
$image='';
$success="<div class='alert alert-success text-success'>The image is has been uploaded </div>";
}else{
$error= "<div class='alert alert-danger text-danger'>The image is not uploaded </div>";
}
}
}
mysqli_close($dbcon);
This a little bit different code to upload image in folder and in database.
<?php include("dbcon.php");
$error=''; $success='';
if(isset($_POST['upload'])){
$image = $_FILES['my-image']['name']; //['my-image'] from input field attribute name="my-image" & ['name'] index from $_FILES array
$image_temp = $_FILES['my-image']['tmp_name']; //['my-image'] from input field attribute name="my-image" & ['temp_name'] index from $_FILES array
$imageSize = $_FILES['my-image']['size']; //['my-image'] from input field attribute name="my-image" & ['size'] index from $_FILES array
$imageError = $_FILES['my-image']['error']; //['my-image'] from input field name attribute name="my-image" & ['error'] index from $_FILES array
$imageType = $_FILES['my-image']['type']; //['my-image'] from input field name attribute name="my-image" & ['type'] index from $_FILES array
//$_FILES is an array to convert into string use explode method get string filename, extension
$file_Ext = explode('.', $image);
// Find The Extension Of file like jpeg png or pdf , use this method to get extension of file like .jpg, .png
$fileExtension = strtolower(end($file_Ext));
// Allowed file type extension to upload
$allowedExtension = ['jpg','jpeg','png'];
if($image==""){
$error= "<div class='alert alert-danger text-danger'>Please select image </div>";
}elseif(!in_array($fileExtension, $allowedExtension)){
$error= "<div class='alert alert-danger text-danger'>This type of image not allowed </div>";
}elseif($imageSize>1000000){
$error= "<div class='alert alert-danger text-danger'>The image size can not be bigger than 1 MB </div>";
}else{
$sql = "INSERT INTO images(`pic_name`) VALUES('$image')";
$run = mysqli_query($dbcon,$sql);
move_uploaded_file($image_temp, "upload/$image");
if($run){
$image='';
$success="<div class='alert alert-success text-success'>The image is has been uploaded </div>";
}else{
$error= "<div class='alert alert-danger text-danger'>The image is not uploaded </div>";
}
}
}
<?php
include("dbcon.php");
$error=''; $success='';
//fetching all images
$sql = "SELECT * FROM images";
$run = mysqli_query($dbcon,$sql);
$data= mysqli_num_rows($run);
mysqli_close($dbcon);
<?php include("fetch.php"); ?>
<?php include("delete.php"); ?>
<?php include("common/header.php"); ?>
<?php include("common/navbar.php"); ?>
<div class="container mt-5">
<div class="text-center"><?php echo $success;?><?php echo $error; ?></div>
<a href="form.php" class="btn btn-info float-right">Upload Image</a>
<div class="row">
<div class="col-md-10" >
<table class="table w-100">
<thead class="bg-dark text-white">
<th>Sr No.<th>
<th>Product Images</th>
<th>Action</th>
</thead>
<tbody>
<?php
if(!empty($data)){
while($row= mysqli_fetch_array($run)){ ?>
<tr>
<td><?php echo $row['id'];?></td>
<td>
<img src="upload/<?php echo $row['pic_name'];?>" width='80' height='100'>
</td>
<td class="pt-5">
<a href="imagetable.php?del=<?php echo $row['id'];?>&del_img=<?php echo $row['pic_name'];?>"
class="btn btn-danger float-right">× Delete
</a>
</td>
</tr>
<?php }
}else{ ?>
<tr>
<td colspan="3" class="text-center">No Image Found </td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
<?php include("common/footer.php"); ?>
Ouput->Images are list in table
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1,
shrink-to-fit=no">
<title>Codetechinfo</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<nav class="navbar navbar-expand-lg navbar-light bg-primary text-white fixed-top">
<a class="navbar-brand text-white" href="./index.php">Learn PHP </a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link text-white" href="./index.php">Home </a>
</li>
</ul>
</div>
</nav>
<br><br>
<div class="copyright py-4 text-center text-white">
<div class="container "><small>© Copyright 2025 by Saten Chauhan</small> </div>
</div>
</body>
</html>