Skip to main content

PHP - File Upload Using PDO Tutorial

Screenshot
Body

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.

tut1

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_name` varchar(100) NOT NULL,
  3. `file_type` varchar(10) NOT NULL,
  4. `date_uploaded` varchar(20) NOT NULL,
  5. `location` text NOT NULL

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.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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.  
  41. $sql = "SELECT * FROM `file`";
  42. $query = $conn->prepare($sql);
  43. $query->execute();
  44.  
  45. while($fetch = $query->fetch()){
  46. ?>
  47.  
  48. <tr>
  49. <td><?php echo $fetch['file_name']?></td>
  50. <td><?php echo $fetch['file_type']?></td>
  51. <td><?php echo $fetch['date_uploaded']?></td>
  52. <td><?php echo $fetch['location']?></td>
  53. </tr>
  54.  
  55. <?php
  56. }
  57. ?>
  58. </tbody>
  59. </table>
  60. </div>
  61. </div>
  62. </div>
  63. </body>
  64. </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. require_once 'conn.php';
  3.  
  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.  
  21. $conn = null;
  22. header('location: index.php');
  23. }
  24. }else{
  25. echo "<center><h3 class='text-danger'>File too large to upload!</h3></center>";
  26. }
  27. }
  28. ?>

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

Add new comment