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.
Table of Contents
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 https://www.apachefriends.org/index.html.
And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
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.
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.
<?php $conn = mysqli_connect("localhost", "root", "", "db_allow"); if(!$conn){ 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"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <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> </div> </form> <?php include 'upload.php'?> </div> <div class="col-md-8"> <div class="table-responsive"> <table class="table table-bordered"> <thead class="alert-info"> <tr> <th>Image</th> <th>Image Name</th> <th>Date Uploaded</th> </tr> </thead> <tbody> <?php require'conn.php'; $query=mysqli_query($conn, "SELECT * FROM `image` ORDER BY `date_uploaded` ASC") or die(mysqli_error()); while($fetch=mysqli_fetch_array($query)){ ?> <tr> <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> </tr> <?php } ?> </tbody> </table> </div> </div> </div> </body> </html>
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.
<?php date_default_timezone_set("Etc/GMT+8"); require_once 'conn.php'; if(ISSET($_POST['upload'])){ $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>"; } }else{ echo "<center><h5 class='text-danger'>Image only is allowed!</h5></center>"; } } ?>