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

PHP - File Upload Using PDO Tutorial

krassablu

Rookie
K Rep
0
0
0
Rep
0
K Vouches
0
0
0
Vouches
0
Posts
143
Likes
87
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a File Upload using PDO. This code can upload a file to the database server with the use of a PDO query. The code uses a PDO query to upload files to the database server with high data protection in order to avoid injection tools. This is a free program, feel free to modify it and use it as your own.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much easier.

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 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_pdo, after that click Import then locate the database file inside the folder of the application then click ok.

2019-09-18_23_29_01-localhost_127.0.0.1_db_upload_pdo_phpmyadmin_4.8.3.png


Or, you can also insert the database table programmatically. Copy/Paste

the SQL

code below in your SQL Page of PHPMyAdmin

.

  1. CREATE

    TABLE

    `file`

    (
  2. `file_

    id`

    int

    (

    11

    )

    NOT

    NULL

    PRIMARY KEY

    AUTO_INCREMENT

    ,
  3. `file_

    name`

    varchar

    (

    100

    )

    NOT

    NULL

    ,
  4. `file_

    type`

    varchar

    (

    10

    )

    NOT

    NULL

    ,
  5. `date_

    uploaded`

    varchar

    (

    20

    )

    NOT

    NULL

    ,
  6. `location`

    text

    NOT

    NULL


  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

Creating the database connection

Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it conn.php

.

  1. <?php
  2. $conn

    =

    new

    PDO(

    'mysql:host=localhost;dbname=db_upload_pdo'

    ,

    'root'

    ,

    ''

    )

    ;

  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 you 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 ng-app="myModule" ng-controller="myController">
  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 - File Upload Usng PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-3">
  18. <form method="POST" enctype="multipart/form-data" action="upload.php">
  19. <div class="form-group">
  20. <label>Upload here</label>
  21. <input name="file" type="file" required="required" class="form-control"/>
  22. </div>
  23. <center><button class="btn btn-primary" name="upload">Upload</button></center>
  24. </form>
  25. </div>
  26. <div class="col-md-9">
  27. <div class="table-responsive">
  28. <table class="table table-bordered">
  29. <thead class="alert-info">
  30. <tr>
  31. <th>File Name</th>
  32. <th>File Type</th>
  33. <th>Date Uploaded</th>
  34. <th>File Path</th>
  35. </tr>
  36. </thead>
  37. <tbody>
  38. <?php
  39. require_once

    'conn.php'

    ;

  40. $sql

    =

    "SELECT * FROM `file`"

    ;
  41. $query

    =

    $conn

    ->

    prepare

    (

    $sql

    )

    ;
  42. $query

    ->

    execute

    (

    )

    ;

  43. while

    (

    $fetch

    =

    $query

    ->

    fetch

    (

    )

    )

    {
  44. ?>

  45. <tr>
  46. <td><?php

    echo

    $fetch

    [

    'file_name'

    ]

    ?>

    </td>
  47. <td><?php

    echo

    $fetch

    [

    'file_type'

    ]

    ?>

    </td>
  48. <td><?php

    echo

    $fetch

    [

    'date_uploaded'

    ]

    ?>

    </td>
  49. <td><?php

    echo

    $fetch

    [

    'location'

    ]

    ?>

    </td>
  50. </tr>

  51. <?php
  52. }
  53. ?>
  54. </tbody>
  55. </table>
  56. </div>
  57. </div>
  58. </div>
  59. </body>
  60. </html>

Creating the Main Function

This code contains the main function of the application. This code will upload an image when the button is clicked. To do that write these block of codes inside the text editor and save it as upload.php

.

  1. <?php
  2. date_default_timezone_set

    (

    "Etc/GMT-8"

    )

    ;
  3. require_once

    'conn.php'

    ;

  4. if

    (

    ISSET

    (

    $_POST

    [

    'upload'

    ]

    )

    )

    {
  5. $file_name

    =

    $_FILES

    [

    'file'

    ]

    [

    'name'

    ]

    ;
  6. $file_temp

    =

    $_FILES

    [

    'file'

    ]

    [

    'tmp_name'

    ]

    ;
  7. $file_size

    =

    $_FILES

    [

    'file'

    ]

    [

    'size'

    ]

    ;
  8. $file_type

    =

    $_FILES

    [

    'file'

    ]

    [

    'type'

    ]

    ;
  9. $date_uploaded

    =

    date

    (

    "Y-m-d"

    )

    ;
  10. $location

    =

    "upload/"

    .

    $file_name

    ;
  11. if

    (

    $file_size

    <

    5242880

    )

    {
  12. if

    (

    move_uploaded_file

    (

    $file_temp

    ,

    $location

    )

    )

    {
  13. try{
  14. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  15. $sql

    =

    "INSERT INTO `file`(file_name, file_type, date_uploaded, location) VALUES ('$file_name

    ', '$file_type

    ', '$date_uploaded

    ', '$location

    ')"

    ;
  16. $conn

    ->

    exec

    (

    $sql

    )

    ;
  17. }

    catch(

    PDOException $e

    )

    {
  18. echo

    $e

    ->

    getMessage

    (

    )

    ;
  19. }

  20. $conn

    =

    null

    ;
  21. header

    (

    'location: index.php'

    )

    ;
  22. }
  23. }

    else

    {
  24. echo

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

    ;
  25. }
  26. }
  27. ?>

DEMO

There you have it we successfully created a File Upload using PDO. I hope that this simple tutorial helps you to what you are looking for. You may also download the working source code that I have created for this tutorial. Just click the download button below. 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 the hidden content.
 

452,292

324,360

324,368

Top