PHP - File Transfer In SQLite3

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

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


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.