PHP - Copy Files In SQLite3

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="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 - Copy Files 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.                                        
  20.                                 <div class="form-group">
  21.                                         <label>File:</label>
  22.                                         <input type="file" name="file" class="form-control" required="required"/>
  23.                                 </div>
  24.                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  25.                                        
  26.                         </form>
  27.                 </div>
  28.                 <br style="clear:both;"/>
  29.                 <div class="col-md-6">
  30.                         <h4>File 1</h3>
  31.                         <div class="table-responsive">
  32.                                 <table class="table table-bordered">
  33.                                         <thead class="alert-info">
  34.                                                 <tr>
  35.                                                         <th>Filename</th>
  36.                                                         <th>Action</th>
  37.                                                 </tr>
  38.                                         </thead>
  39.                                         <tbody>
  40.                                                 <?php
  41.                                                         $files = scandir('file1/');
  42.                                                         foreach ($files as $file){
  43.                                                                 if($file != '.' && $file !='..'){
  44.                                                 ?>
  45.                                                 <tr>
  46.                                                         <td><?php echo $file?></td>
  47.                                                         <td>
  48.                                                                 <?php
  49.                                                                         if(file_exists("file2/".$file)){       
  50.                                                                 ?>
  51.                                                                         <center><button name="copy" class="btn btn-success" disabled="disa"><span class="glyphicon glyphicon-arrow-right"></span> Copied</button></center>
  52.                                                                 <?php
  53.                                                                         }else{
  54.                                                                 ?>
  55.                                                                 <form method="POST" action="copy.php">
  56.                                                                         <center><button name="copy" class="btn btn-success"><span class="glyphicon glyphicon-arrow-right"></span> Copy</button></center>
  57.                                                                         <input type="hidden" name="file" value="<?php echo $file?>"/>
  58.                                                                 </form>
  59.                                                                 <?php
  60.                                                                         }
  61.                                                                 ?>
  62.                                                         </td>
  63.                                                 </tr>
  64.                                                 <?php
  65.                                                                 }
  66.                                                         }
  67.                                                 ?>
  68.                                        
  69.                                         </tbody>
  70.                                 </table>
  71.                         </div>
  72.                 </div>
  73.                 <div class="col-md-6">
  74.                         <h4>File 2</h3>
  75.                         <div class="table-responsive">
  76.                                 <table class="table table-bordered">
  77.                                         <thead class="alert-info">
  78.                                                 <tr>
  79.                                                         <th>Filename</th>
  80.                                                 </tr>
  81.                                         </thead>
  82.                                         <tbody>
  83.                                                 <?php
  84.                                                         $files = scandir('file2/');
  85.                                                         foreach ($files as $file){
  86.                                                                 if($file != '.' && $file !='..'){
  87.                                                 ?>
  88.                                                 <tr>
  89.                                                         <td><?php echo $file?></td>
  90.                                                 </tr>
  91.                                                 <?php
  92.                                                                 }
  93.                                                         }
  94.                                                 ?>
  95.                                         </tbody>
  96.                                 </table>
  97.                         </div>
  98.                 </div>
  99.         </div>
  100. </body>
  101. </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