PHP - Copy Files In SQLite3

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

In this tutorial we will create a Copy Files In SQLite3 using PHP. This code will dynamically copy a file in SQLite database when user click the copy button in the table. This code use PHP POST method to call a function that can copy a file to a different folder using copy() function by setting 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 `file1`(image_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, image_name TEXT, location TEXT)";
  4.  
  5. $conn->exec($query);
  6. $query="CREATE TABLE IF NOT EXISTS `file2`(image_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, image_name 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="<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 - Copy Files 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.  
  21. <div class="form-group">
  22. <label>File:</label>
  23. <input type="file" name="file" class="form-control" required="required"/>
  24. </div>
  25. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  26.  
  27. </form>
  28. </div>
  29. <br style="clear:both;"/>
  30. <div class="col-md-6">
  31. <h4>File 1</h3>
  32. <div class="table-responsive">
  33. <table class="table table-bordered">
  34. <thead class="alert-info">
  35. <tr>
  36. <th>Filename</th>
  37. <th>Action</th>
  38. </tr>
  39. </thead>
  40. <tbody>
  41. <?php
  42. $files = scandir('file1/');
  43. foreach ($files as $file){
  44. if($file != '.' && $file !='..'){
  45. ?>
  46. <tr>
  47. <td><?php echo $file?></td>
  48. <td>
  49. <?php
  50. if(file_exists("file2/".$file)){
  51. ?>
  52. <center><button name="copy" class="btn btn-success" disabled="disa"><span class="glyphicon glyphicon-arrow-right"></span> Copied</button></center>
  53. <?php
  54. }else{
  55. ?>
  56. <form method="POST" action="copy.php">
  57. <center><button name="copy" class="btn btn-success"><span class="glyphicon glyphicon-arrow-right"></span> Copy</button></center>
  58. <input type="hidden" name="file" value="<?php echo $file?>"/>
  59. </form>
  60. <?php
  61. }
  62. ?>
  63. </td>
  64. </tr>
  65. <?php
  66. }
  67. }
  68. ?>
  69.  
  70. </tbody>
  71. </table>
  72. </div>
  73. </div>
  74. <div class="col-md-6">
  75. <h4>File 2</h3>
  76. <div class="table-responsive">
  77. <table class="table table-bordered">
  78. <thead class="alert-info">
  79. <tr>
  80. <th>Filename</th>
  81. </tr>
  82. </thead>
  83. <tbody>
  84. <?php
  85. $files = scandir('file2/');
  86. foreach ($files as $file){
  87. if($file != '.' && $file !='..'){
  88. ?>
  89. <tr>
  90. <td><?php echo $file?></td>
  91. </tr>
  92. <?php
  93. }
  94. }
  95. ?>
  96. </tbody>
  97. </table>
  98. </div>
  99. </div>
  100. </div>
  101. </body>
  102. </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. $filename = $_FILES['file']['name'];
  5. $filesize = $_FILES['file']['size'];
  6. $filetemp = $_FILES['file']['tmp_name'];
  7.  
  8. if($filesize > 500000){
  9. echo "<script>alert('File too large to upload')</script>";
  10. echo "<script>window.location = 'index.php'</script>";
  11. }else{
  12. $file = explode(".", $filename);
  13. $file_ext = end($file);
  14. $ext = array("png", "jpg", "jpeg");
  15.  
  16. if(in_array($file_ext, $ext)){
  17. $location = "file1/".$filename;
  18. if(move_uploaded_file($filetemp, $location)){
  19. $query="INSERT INTO `file1` (image_name, location) VALUES('$filename', '$location')";
  20. $conn->exec($query);
  21. echo "<script>alert('File Saved!')</script>";
  22. echo "<script>window.location = 'index.php'</script>";
  23. }
  24. }else{
  25. echo "<script>alert('Only images allowed')</script>";
  26. echo "<script>window.location = 'index.php'</script>";
  27. }
  28. }
  29. }
  30. ?>

Creating the Main Function

This code contains the main function of the application. This code will copy 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 copy.php.

  1. <?php
  2. require_once'conn.php';
  3. if(ISSET($_POST['copy'])){
  4. $file = "file1/".$_POST['file'];
  5. $newfile = "file2/".$_POST['file'];
  6.  
  7. if(!copy($file, $newfile)){
  8. echo "<script>alert('Failed to copy ".$file."')</script>";
  9. echo "<script>window.location = 'index.php'</script>";
  10. }else{
  11. $query="INSERT INTO `file2` (image_name, location) VALUES('$_POST[file]', '$newfile')";
  12. $conn->exec($query);
  13. echo "<script>alert('File copied!')</script>";
  14. echo "<script>window.location = 'index.php'</script>";
  15. }
  16. }
  17. ?>

There you have it we successfully created a Copy Files 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.