Skip to main content

PHP - File Transfer In SQLite3


In this tutorial we will create a File Transfer In SQLite3 using PHP. This code will dynamically move a file in SQLite database when user click the transfer button in the table. This code use PHP POST method to call a function that can transfer file to different folder using rename() function by adding the old file and new file as a parameter. This a free program, you can apply this to your system as your own.

We will be using SQLite that 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.

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 SQLite3('db/db_image') or die("Unable to open database!");
  3. $query="CREATE TABLE IF NOT EXISTS `image`(image_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, image_name TEXT, location TEXT)";
  4.  
  5. $conn->exec($query);
  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>
  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-2"></div>
  14. <div class="col-md-8 well">
  15. <h3 class="text-primary">PHP - File Transfer In SQLite3</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form action="save_file.php" method="POST" enctype="multipart/form-data">
  19. <div class="form-group">
  20. <label>File:</label>
  21. <input type="file" name="file" class="form-control" required="required"/>
  22. </div>
  23. <button class="btn btn-primary" name="save">Save</button>
  24. </form>
  25. </div>
  26. <br style="clear:both;"/>
  27. <div class="col-md-8">
  28. <h4>Folder 1</h4>
  29. <div class="table-responsive">
  30. <table class="table table-bordered">
  31. <thead class="alert-info">
  32. <th>Filename</th>
  33. <th>Location</th>
  34. <th>Action</th>
  35. </thead>
  36. <tbody style="background-color:#fff;">
  37. <?php
  38. $files = scandir('folder1/');
  39. foreach($files as $file){
  40. if($file != "." && $file != ".."){
  41. ?>
  42. <tr>
  43. <td><?php echo $file?></td>
  44. <td><?php echo realpath('folder1/'.$file)?></td>
  45. <td>
  46. <form method="POST" action="transfer.php">
  47. <input type="hidden" name="file" value="<?php echo $file?>"/>
  48. <button class="btn btn-primary" name="transfer"><span class="glyphicon glyphicon-arrow-right"></span> Transfer</button>
  49. </form>
  50. </td>
  51. </tr>
  52. <?php
  53. }
  54. }
  55. ?>
  56. </tbody>
  57. </table>
  58. </div>
  59. </div>
  60. <div class="col-md-4">
  61. <h4>Folder 2</h4>
  62. <div class="table-responsive">
  63. <table class="table table-bordered">
  64. <thead class="alert-info">
  65. <th>Filename</th>
  66. <th>Location</th>
  67. </thead>
  68. <tbody style="background-color:#fff;"
  69. <?php
  70. $files = scandir('folder2/');
  71. foreach($files as $file){
  72. if($file != "." && $file != ".."){
  73. ?>
  74. <tr>
  75. <td><?php echo $file?></td>
  76. <td><?php echo realpath('folder2/'.$file)?></td>
  77. </tr>
  78. <?php
  79. }
  80. }
  81. ?>
  82. </tbody>
  83. </table>
  84. </div>
  85. </div>
  86. </div>
  87. </body>
  88. <script src="js/jquery-3.2.1.min.js"></script>
  89. <script src="js/bootstrap.js"></script>
  90. </html>

Creating the SQLite Query

This code contains the SQLite query of the application. This code will store the data inputs to SQLite database. To do this just copy and write these block of codes as shown below inside the text editor, then save it as save_file.php.

  1. <?php
  2. require_once'conn.php';
  3. if(ISSET($_POST['save'])){
  4. $file_name = $_FILES['file']['name'];
  5. $file_size = $_FILES['file']['size'];
  6. $file_temp = $_FILES['file']['tmp_name'];
  7.  
  8. if($file_size > 500000){
  9. echo "<script>alert('File too large to upload')</script>";
  10. echo "<script>window.location = 'index.php'</script>";
  11. }else{
  12. $exp = explode(".", $file_name);
  13. $ext = end($exp);
  14. $image = time().'.'.$ext;
  15. $ext_allowed = array("png", "gif", "jpeg", "jpg");
  16. $location = "folder1/".$image;
  17. if(in_array($ext, $ext_allowed)){
  18. if(move_uploaded_file($file_temp, $location)){
  19. $query="INSERT INTO `image` (image_name, location) VALUES('$image', '$location')";
  20.  
  21. $conn->exec($query);
  22.  
  23. echo "<script>alert('Image uploaded!')</script>";
  24. echo "<script>window.location='index.php'</script>";
  25. }
  26. }else{
  27. echo "<script>alert('Only image to upload!')</script>";
  28. echo "<script>window.location='index.php'</script>";
  29. }
  30. }
  31. }
  32. ?>

Creating the Main Function

This code contains the main function of the application. This code will transfer a file when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor, then save it as transfer.php.

  1. <?php
  2. require_once'conn.php';
  3. if(ISSET($_POST['transfer'])){
  4. $file = "folder1/".$_POST['file'];
  5. $newfile = "folder2/".$_POST['file'];
  6.  
  7. if(!rename($file, $newfile)){
  8. echo "<script>alert('Failed to move ".$file."')</script>";
  9. echo "<script>window.location = 'index.php'</script>";
  10. }else{
  11. $query="UPDATE `image` set image_name='$_POST[file]', location='$newfile'";
  12. $conn->exec($query);
  13.  
  14. echo "<script>alert('Successfully Transfer!')</script>";
  15. echo "<script>window.location = 'index.php'</script>";
  16. }
  17. }
  18. ?>

There you have it we successfully created a File Transfer In SQLite3 using PHP. 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!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.