PHP Ajax Image Upload Tutorial


Share:
Published By: Admin, Published On: Apr 22, 2021, Category: PHP

Hi Dev,

In this blog, I will give you how to upload image 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 images in MySQL Database using ajax.

Image Upload in php 8 with ajax. I am going to show you about image upload in php 8 utilizing jquery ajax. this example will avail you php 8 upload image to database with jquery ajax. This article goes in detailed on how to upload and display image in php 8 with ajax. Here, Engendering a rudimental example of php 8 image upload with preview utilizing jquery ajax.

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

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

Create Database

In this step you can create database `php_curd`.

Create `table_name` inside the database.

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

CREATE TABLE `user` (
    `id` int(11) NOT NULL,
    `file` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Database Configuration conn.php
<?php

    $servername = 'localhost';
    $username   = 'root';
    $password   = 'root';
    $dbname     = "php_curd";
    $conn       = mysqli_connect($servername,$username,$password,$dbname);
    
    if(!$conn){
        die('Could not Connect MySql Server:' .mysql_error());
    }

?>
index.php
    
<html>
<head>
    PHP AJAX Image Upload Tutorial - MyWebtuts.com
    
    
    
</head>
<body class="bg-dark">
    

PHP 8 Ajax Image Upload - MyWebtuts.com

No Image
</body> </html>
upload.php
<?php

    include 'conn.php';
    if ( 0 < $_FILES['file']['error'] ) {
        echo 'Error: ' . $_FILES['file']['error'] . '
'; } else { move_uploaded_file($_FILES['file']['tmp_name'], 'files/' . $_FILES['file']['name']); $targetPath = "images/".$_FILES['file']['name']; $q = "INSERT INTO `user`(`file`) VALUES ('{$targetPath}')"; $sql = mysqli_query($conn, $q); } ?>

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:

http://localhost:8000

I Hope It will help you..

Share: