Allow Only Image To Be Upload In PHP MySQL | CampCodes

Allow Only Image To Be Upload in PHP MySQL

June 6, 2020
php file upload

In this tutorial, we will create an Allow the Only Image To Be Upload using PHP. This code will allow the user to upload any image file to the database server. The system uses the PHP POST method to initiate a specific function that can upload an only image file to the MySQLi server by using the in_array() service by providing the file extension and an array list of image formats. This is user-friendly kind of program feel free to modify and apply it to your code.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server

And, this is the link for the bootstrap that I used for the layout design

Creating Database

Open your database web server then create a database name in it db_allow, after that, click Import then locates the database file inside the folder of the application then click ok.

only image to be upload php

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

    $conn = mysqli_connect("localhost", "root", "", "db_allow");
        die("Error: Failed to connect to database!");

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Allow Only Image To Be Upload</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-4">
            <form method="POST" enctype="multipart/form-data">
                <div class="form-inline">
                    <input type="file" name="image"/>
                    <br />
                    <center><button class="btn btn-primary btn-sm" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button></center>
            <?php include 'upload.php'?>
        <div class="col-md-8">
            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead class="alert-info">
                            <th>Image Name</th>
                            <th>Date Uploaded</th>
                            $query=mysqli_query($conn, "SELECT * FROM `image` ORDER BY `date_uploaded` ASC") or die(mysqli_error());
                            <td><img src="upload/<?php echo $fetch['image_name']?>" height="80" width="120"/></td>
                            <td><?php echo $fetch['image_name']?></td>
                            <td><?php echo $fetch['date_uploaded']?></td>

Creating the Main Function

This code contains the main function of the application. This code will upload only accept image file. To do that just copy and write this block of codes inside the text editor, then save it as upload.php.

    require_once 'conn.php';
        $file_name = $_FILES['image']['name'];
        $file_temp = $_FILES['image']['tmp_name'];
        $date_upload = date("Y-m-d");
        $allowed_ext = array("jpeg", "jpg", "gif", "png");
        $exp = explode(".", $file_name);
        $ext = end($exp);
        $path = "upload/".$file_name;
        if(in_array($ext, $allowed_ext)){
            if(move_uploaded_file($file_temp, $path)){
                mysqli_query($conn, "INSERT INTO `image` VALUES('', '$file_name', '$date_upload')") or die(mysqli_error());
                echo "<center><h5 class='text-success'>Image uploaded!</h5></center>";
            echo "<center><h5 class='text-danger'>Image only is allowed!</h5></center>";
There you have it we successfully created Allow Only Image To Be Upload using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *