PHP 8 Ajax File Upload Example




PHP 8 Ajax File Upload Example

Hi Dev,

In this blog, I will give you how to upload file in php 8 with ajax. In this tutorial PHP 8 file uploading and storing tutorial with ajax and mysql database. In this tutorial, we will learn how to upload files and files in MySQL Database using ajax.

File Upload in php 8 with ajax. I am going to show you about file upload in php 8 using jquery ajax. this example will help you php 8 upload file to database with jquery ajax. This article goes in detailed on how to upload and display file in php 8 with ajax. Here, Creating a basic example of php 8 file upload with preview using jquery ajax.

I created simple form with file input. So you have to simple select file and then it will upload in "files" directory of this folder. So you have to simple follow bellow step and get file upload in php 8 ajax application.

Here, I will give you full example for simply file Upload using php 8 jquery ajax as bellow.

Create Database

In this step you can create database `php_crud`.

Create `table_name` inside the database.

You can execute the following command to create the table columns to store the files in the database.


`id` int(11) NOT NULL,

`file` varchar(255) NOT NULL


Database Configuration



$servername = 'localhost';

$username = 'root';

$password = 'root';

$dbname = "php_crud";

$conn = mysqli_connect($servername,$username,$password,"$dbname");


die('Could not Connect MySql Server:' .mysql_error());






<title>PHP AJAX file Upload</title>

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

<script src=""></script>

<style type="text/css">



width: 100%;

height: 50px;


line-height: 50px;

font-weight: bold;

color: #C0C0C0;

background-color: #F0E8E0;

border-bottom-left-radius: 4px;

border-top-left-radius: 4px;


.file-preview {


height: 100%;

border-bottom-left-radius: 4px;

border-top-left-radius: 4px;





<div class="container mt-5">

<div class="row">

<div class="col-md-6 offset-3 mt-5">

<div class="card">

<div class="card-header bg-info">

<h4 class="card-heading text-white">PHP 8 Ajax file Upload -</h4>


<div class="card-body">

<?php if(!empty($_SESSION['message'])) {?>

<div class="alert text-center alert-success" role="alert">


echo $_SESSION['message'];




<?php }?>

<form id="uploadForm" action="upload.php" method="post">

<div class="row">

<div class="col-md-12 mb-1">

<div id="targetLayer">No File</div>


<div class="col-md-12">

<div class="form-group">

<input name="userfile" type="file" class="inputFile form-control" />



<div class="col-md-12">

<div class="form-group text-center">

<input type="submit" value="Save" class="btnSubmit btn btn-success" />










<script type="text/javascript">

$(document).ready(function (e) {

$("#uploadForm").on('submit',(function(e) {



url: "upload.php",

type: "POST",

data: new FormData(this),

contentType: false,

cache: false,


success: function(data)




error: function()











include 'config.php';

// Send email

if(is_array($_FILES)) {

if(is_uploaded_file($_FILES['userFile']['tmp_name'])) {

$sourcePath = $_FILES['userFile']['tmp_name'];

$targetPath = "files/".$_FILES['userFile']['name'];

if(move_uploaded_file($sourcePath,$targetPath)) {

// Store contactor data in database

$q = "INSERT INTO user(file)

VALUES ('{$targetPath}')";

$sql = mysqli_query($conn, $q);

if(!$sql) {

die("MySQL query failed.");



<span class="file-preview" class="upload-preview"> File Upload Successfully.</span>






Now we are ready to run our example so run bellow command for quick run:

php -S localhost:8000

Now you can open bellow URL on your browser:


It will help you....

#PHP 8