• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Form File Upload Using Ajax

Bagzi

Code Coverage Analyst
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
111
Likes
169
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Form File Upload using Ajax. This code will upload a file to the MySQLi database without refreshing the browser after submission.The code use jQuery ajax to upload a file to MySQLi server without page refreshing and modify HTML table to display the retrieve data on it. This is a user-friendly kind of program feel free user it in your program.

We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can retrieve DOM and manipulate each properties of an element based on different criteria such as id, class, etc.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, 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_upload_ajax, after that click Import then locate the database file inside the folder of the application then click ok.
2019-10-05_00_04_52-localhost_127.0.0.1_db_upload_ajax_phpmyadmin_4.8.3.png

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.
  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'db_upload_ajax'

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

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.
  1. <!

    DOCTYPE html>
  2. <

    html lang=

    "en"

    >
  3. <

    head>
  4. <

    meta charset=

    "UTF-8"

    name=

    "viewport"

    content=

    "width=device-width, initial-scale=1"

    />
  5. <

    link

    rel=

    "stylesheet"

    type=

    "text/css"

    href=

    "css/bootstrap.css"

    />
  6. </

    head>
  7. <

    body>
  8. <

    nav class

    =

    "navbar navbar-default"

    >
  9. <

    div class

    =

    "container-fluid"

    >
  10. <

    a class

    =

    "navbar-brand"

    href=

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a>
  11. </

    div>
  12. </

    nav>
  13. <

    div class

    =

    "col-md-3"

    ></

    div>
  14. <

    div class

    =

    "col-md-6 well"

    >
  15. <

    h3 class

    =

    "text-primary"

    >

    PHP -

    Form File

    Upload Using Ajax</

    h3>
  16. <

    hr style=

    "border-top:1px dotted #ccc;"

    />
  17. <

    div class

    =

    "col-md-4"

    >
  18. <

    form>
  19. <

    input type=

    "file"

    id=

    "file"

    />
  20. <

    br />
  21. <

    center><

    button type=

    "button"

    class

    =

    "btn btn-primary"

    id=

    "upload"

    >

    Upload</

    button></

    center>
  22. <

    br />
  23. <

    div id=

    "msg"

    ></

    div>
  24. </

    form>
  25. </

    div>
  26. <

    div class

    =

    "col-md-8"

    >
  27. <

    table class

    =

    "table table-bordered"

    >
  28. <

    thead class

    =

    "alert-info"

    >
  29. <

    tr>
  30. <

    th>

    File

    Name</

    th>
  31. <

    th>

    File

    Path</

    th>
  32. </

    tr>
  33. </

    thead>
  34. <

    tbody id=

    "result"

    ></

    tbody>
  35. </

    table>
  36. </

    div>
  37. </

    div>
  38. <

    script src=

    "js/jquery-3.2.1.min.js"

    >

    </script>
  39. <

    script src=

    "js/script.js"

    >

    </script>
  40. </

    body>
  41. </

    html>

Creating the Main Function

This code contains the main function of the application. This code will upload a file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below
data.php
  1. <?php
  2. require

    'conn.php'

    ;
  3. $query

    =

    mysqli_query

    (

    $conn

    ,

    'SELECT * FROM `file`'

    )

    ;
  4. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  5. ?>
  6. <tr>
  7. <td><?php

    echo

    $fetch

    [

    'filename'

    ]

    ?>

    </td>
  8. <td><?php

    echo

    $fetch

    [

    'location'

    ]

    ?>

    </td>
  9. </tr>
  10. <?php
  11. }
  12. ?>

upload.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    !

    empty

    (

    $_FILES

    [

    'file'

    ]

    )

    )

    {
  4. $file_name

    =

    $_FILES

    [

    'file'

    ]

    [

    'name'

    ]

    ;
  5. $file_temp

    =

    $_FILES

    [

    'file'

    ]

    [

    'tmp_name'

    ]

    ;
  6. $file_size

    =

    $_FILES

    [

    'file'

    ]

    [

    'size'

    ]

    ;

  7. $exp

    =

    explode

    (

    "."

    ,

    $file_name

    )

    ;
  8. $ext

    =

    end

    (

    $exp

    )

    ;
  9. $file

    =

    time

    (

    )

    .

    '.'

    .

    $ext

    ;
  10. $location

    =

    "upload/"

    .

    $file

    ;

  11. if

    (

    $file_size

    <

    5242880

    )

    {
  12. move_uploaded_file

    (

    $file_temp

    ,

    $location

    )

    ;
  13. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `file` VALUES('', '$file

    ', '$location

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  14. echo

    "success"

    ;
  15. }

    else

    {
  16. echo

    "error2"

    ;
  17. }
  18. }

    else

    {
  19. echo

    "error1"

    ;
  20. }


  21. ?>

script.js
Note: To make the script works make sure you save this file inside the js directory.
  1. $(

    document)

    .ready

    (

    function

    (

    )

    {
  2. display_data(

    )

    ;

  3. function

    display_data(

    )

    {
  4. $.ajax

    (

    {
  5. url:

    'data.php'

    ,
  6. type:

    'POST'

    ,
  7. data:

    {

    res:

    1

    }

    ,
  8. success:

    function

    (

    data)

    {
  9. $(

    '#result'

    )

    .html

    (

    data)

    ;
  10. }
  11. }

    )

    ;
  12. }

  13. $(

    '#upload'

    )

    .on

    (

    'click'

    ,

    function

    (

    )

    {
  14. $(

    this

    )

    .attr

    (

    'disabled'

    ,

    'disabled'

    )

    ;
  15. var

    file =

    $(

    '#file'

    )

    ;
  16. var

    file_length =

    file[

    0

    ]

    .files

    .length

    ;
  17. var

    file_data =

    file.prop

    (

    'files'

    )

    [

    0

    ]

    ;

  18. var

    formData =

    new

    FormData(

    )

    ;
  19. formData.append

    (

    'file'

    ,

    file_data)

    ;
  20. $.ajax

    (

    {
  21. url:

    "upload.php"

    ,
  22. type:

    "POST"

    ,
  23. data:

    formData,
  24. contentType:

    false

    ,
  25. cache:

    false

    ,
  26. processData:

    false

    ,
  27. success:

    function

    (

    data)

    {
  28. if

    (

    data==

    "success"

    )

    {
  29. $(

    "#msg"

    )

    .empty

    (

    )

    ;
  30. $(

    "<center class='text-success'>Successfully uploaded!</center>"

    )

    .appendTo

    (

    $(

    "#msg"

    )

    )

    ;
  31. $(

    '#file'

    )

    .val

    (

    ''

    )

    ;
  32. display_data(

    )

    ;
  33. }

    else

    if

    (

    data==

    "error1"

    )

    {
  34. $(

    "#msg"

    )

    .empty

    (

    )

    ;
  35. $(

    "<center class='text-danger'>Please upload file first!</center>"

    )

    .appendTo

    (

    $(

    "#msg"

    )

    )

    ;
  36. $(

    '#file'

    )

    .val

    (

    ''

    )

    ;
  37. }

    else

    if

    (

    data==

    "error2"

    )

    {
  38. $(

    "#msg"

    )

    .empty

    (

    )

    ;
  39. $(

    "<center class='text-danger'>File too large to upload!"

    )

    .appendTo

    (

    $(

    "#msg"

    )

    )

    ;
  40. $(

    '#file'

    )

    .val

    (

    ''

    )

    ;
  41. }

  42. $(

    '#upload'

    )

    .removeAttr

    (

    'disabled'

    )

    ;
  43. }
  44. }

    )

    ;
  45. }

    )

    ;
  46. }

    )

    ;

There you have it we successfully created Form File Upload using Ajax. 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!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top