PHP - Upload Files To SQLite Using PDO

Submitted by: 
Language: 
Visitors have accessed this post 305 times.

In this tutorial we will create a Upload Files To SQLite Using PDO. This code can be use for storing of list of data. PHP is a server-side scripting language designed primarily for web development. SQLite provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP. So Let's do the coding...

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/.

Installing SQLite Browser
We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.

Setting Up SQLite

First, we are going to enable SQLite 3 in our PHP.

1. Open localhost server folder XAMPP, etc and locate php.ini.
2. Open php.ini and enable sqlite3 by removing the semicolon in the line.
tut1
3. Save changes and Restart Server.

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 = new PDO('sqlite:db/db_file.sqlite3');
  3.  
  4. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  5.  
  6. $query = "CREATE TABLE IF NOT EXISTS file (file_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, file_name TEXT, file_type TEXT, file_size TEXT, location TEXT)";
  7.  
  8. $conn->exec($query);
  9. ?>

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>
  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 - Upload Files To SQLite Using PDO</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form class="form-inline" method="POST" enctype="multipart/form-data" action="save_file.php">
  18. <label><input type="file" name="file" class="form-control"/></label>
  19. <button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  20. </form>
  21. <br /><br />
  22. <div class="table-responsive">
  23. <table class="table table-bordered">
  24. <thead class="alert-info">
  25. <tr>
  26. <th>File Name</th>
  27. <th>File Type</th>
  28. <th>File Size</th>
  29. <th>Location</th>
  30. </tr>
  31. </thead>
  32. <tbody style="background-color:#fff;">
  33. <?php
  34. require 'conn.php';
  35. $query = $conn->prepare("SELECT * FROM `file` ORDER BY `file_type` ASC");
  36. $query->execute();
  37. while($fetch = $query->fetch()){
  38. ?>
  39. <tr>
  40. <td><?php echo $fetch['file_name']?></td>
  41. <td><?php echo $fetch['file_type']?></td>
  42. <td><?php echo $fetch['file_size']?></td>
  43. <td><?php echo $fetch['location']?></td>
  44. </tr>
  45. <?php
  46. }
  47. ?>
  48. </tbody>
  49. </table>
  50. </div>
  51. </div>
  52. </body>
  53. </html>

Creating the Main Function

This code contains the main function of the application. This code will upload files into the SQLite database. To do this just copy and write these block of codes as shown below inside the text editor and save it as save_file.php.

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['upload'])){
  5. $file = $_FILES['file'];
  6.  
  7. $file_name = $file['name'];
  8. $file_temp = $file['tmp_name'];
  9. $file_type = $file['type'];
  10. $file_size = $file['size'];
  11. $exp = explode('.', $file_name);
  12. $name = $exp[0];
  13. $path = "upload/".$file_name;
  14.  
  15. if(move_uploaded_file($file_temp, $path)){
  16. $query = "INSERT INTO `file` (file_name, file_type, file_size, location) VALUES(:file_name, :file_type, :file_size, :location)";
  17. $stmt = $conn->prepare($query);
  18. $stmt->bindParam(':file_name', $name);
  19. $stmt->bindParam(':file_type', $file_type);
  20. $stmt->bindParam(':file_size', $file_size);
  21. $stmt->bindParam(':location', $path);
  22.  
  23. $stmt->execute();
  24. $conn = null;
  25.  
  26. echo "<script>alert('File Uploaded')</script>";
  27. echo "<script>window.location='index.php'</script>";
  28. }
  29. }
  30. ?>

There you have it we successfully created a Upload Files To SQLite Using PDO. 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!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.