File Deletion Using PHP Source Code

In this tutorial we will create a File Deletion using PHP. This code can delete a text file when user click the delete button. The code use PHP href or Hyper Text Referencing to direct the request to other page where the binded filename is attach in order to delete the file with unlink(). This is a user-friendly kind of program feel free to modify it. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.

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

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 your text editor, then save it as shown below. 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">File Deletion Using PHP Source Code</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div>
  18.                 <div class="col-md-4">
  19.                         <form method="POST" action="add_file.php">
  20.                                 <div class="form-group">
  21.                                         <label>Filename:</label>
  22.                                         <input type="text" name="name" class="form-control"/>
  23.                                 </div>
  24.                                 <center><button class="btn btn-primary">Create File</button></center>
  25.                         </form>
  26.                 </div>
  27.                 <div class="col-md-8">
  28.                         <table class="table table-bordered">
  29.                                 <thead class="alert-info">
  30.                                         <tr>
  31.                                                 <th>Name</th>
  32.                                                 <th>Action</th>
  33.                                         </tr>
  34.                                 </thead>
  35.                                 <tbody>
  36.                                         <?php
  37.                                                 $full_path = "files";
  38.                                                
  39.                                                 $dir = opendir($full_path);
  40.                                
  41.                                                 while(($file = readdir($dir)) !== FALSE){
  42.                                                         if($file == "." || $file == "..")
  43.                                                                
  44.                                                         continue;
  45.                                                 ?>
  46.                                                         <tr>
  47.                                                                 <td><?php echo $file?></td>
  48.                                                                 <td colspan="2"><center><a class="btn btn-danger" href="delete_file.php?filename=<?php echo $file?>"><span class="glyphicon glyphicon-trash"></span> Delete</a></center></td>
  49.                                                         </tr>  
  50.                                         <?php
  51.                                                
  52.                                                 }
  53.                                                
  54.                                                 closedir($dir);
  55.                                         ?>
  56.                                
  57.                                 </tbody>
  58.                         </table>
  59.                 </div>
  60.         </div>
  61.        
  62.        
  63. </body>
  64. </html>
add_file.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">File Deletion Using PHP Source Code</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <?php
  18.                         $path = "files";
  19.                         $filename = $_POST['name'].".txt";
  20.                         fopen($path."/".$filename, 'w');
  21.                 ?>
  22.                 <form method="POST" action="save_file.php">
  23.                         <label>Enter a Text</label>
  24.                         <textarea name="content" style="width:100%; height:100px; resize:none;"></textarea>
  25.                         <input type="hidden" name="filename" value="<?php echo $filename?>"/>
  26.                         <button type="submit" class="btn btn-primary">Save</button>
  27.                 </form>
  28.         </div>
  29.        
  30.        
  31. </body>
  32. </html>

Creating the Main Function

This code contains the main function of the application. This code will delete a text file when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. save_file.php
  1. <?php
  2.  
  3.         if($_POST['content'] != ""){
  4.                 $content = $_POST['content'];
  5.                 $file_name = $_POST['filename'];
  6.                 $path = "files";
  7.                 $file = fopen($path."/".$file_name, 'w');
  8.                 fwrite($file, $content);
  9.                 fclose($file);
  10.                 header("location:index.php");
  11.         }else{
  12.                 echo "Error!";
  13.                 echo "<a href='index.php'>Back</a>";
  14.                
  15.         }
  16.  
  17. ?>
delete_file.php
  1. <?php
  2.         $file=$_REQUEST['filename'];
  3.        
  4.         if($file){
  5.                 unlink('files/'.$file);
  6.                 header('location:index.php');
  7.         }
  8.  
  9. ?>
There you have it we successfully created File Deletion 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