Single - File Upload Pluggin with Ajax & PHP



   frontend file

 <!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

<h2>Upload File:</h2>

    <form id="up">

        <input type="file" name="upload"><br><br>

        <button type="button" onclick="upload_data();">UPLOAD</button>

    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

    <script>

        function upload_data(){

            var file = new FormData(up);

                console.log(file);

                    $.ajax({

                        data : file,

                        type : "POST",

                        url : "backend.php",

                        processData : false,

                        contentType : false,

                        success: function (res) {

                            console.log(res);

                            

                        }

                    })

        }

    </script>

</body>

</html>


Backend file


<?php

$conn = mysqli_connect("localhost","root","","fileupload_practice_2") or die ("connection failed");

$file = $_FILES['upload']['name'];

$temp = $_FILES['upload']['tmp_name'];

move_uploaded_file($temp,"img/".$file);

$query = "INSERT INTO file_table (file) VALUES('$file')";

$fire = mysqli_query($conn,$query);

if ($file) {

    echo "uploaded";

}else {

    echo "Not upload";

}

?>

Comments

Popular posts from this blog

C.R.U.D Oparetion in (PHP Code)

Search Dropdown - jQuery

JavaScript Form Validation Code