Skip to main content

PHP - Dynamically Remove File


In this tutorial we will create a Dynamically Remove File using PHP. This code will dynamically remove a file when user click the button within the range of the table row. This code use a simple href or hypertext reference to call a script within the server that send the file data which remove the file using unlink() and by adding the file path as a parameter.This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.

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 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">PHP - Dynamically Remove File</h3>
  16. <hr style="border-top:1px dottec #ccc;"/>
  17. <div class="col-md-8">
  18. <table class="table table-bordered">
  19. <thead class="alert-info">
  20. <tr>
  21. <th>File Name</th>
  22. <th>Action</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <?php
  27. $full_path = "files";
  28.  
  29. $dir = opendir($full_path);
  30.  
  31. while(($file = readdir($dir)) !== FALSE){
  32. if($file == "." || $file == "..")
  33.  
  34. continue;
  35. ?>
  36. <tr>
  37. <td><?php echo $file?></td>
  38. <td><a class="btn btn-danger btn-sm" href="delete_file.php?file_name=<?php echo $file?>">Delete</a></td>
  39. </tr>
  40. <?php
  41.  
  42. }
  43.  
  44. closedir($dir);
  45. ?>
  46.  
  47. </tbody>
  48. </table>
  49. </div>
  50. <div class="col-md-4">
  51. <div class="form-inline">
  52. <form method="POST" action="create_file.php">
  53. <label style="font-size:18px;">Filename:</label>
  54. <input type="text" name="filename" class="form-control" required="required"/>
  55. <br /><br />
  56. <label>Enter a Text</label>
  57. <textarea name="content" style="width:100%; height:100px; resize:none;"></textarea>
  58. <br /><br />
  59. <center><button class="btn btn-primary" name="create">Create File</button></center>
  60. </form>
  61. </div>
  62. </div>
  63. </div>
  64.  
  65.  
  66. </body>
  67. </html>

Creating the Adding Function

This code contains the add function of the application. This code will add a new file when user supply the require information. To do that just copy and write this block of codes inside the text editor, then save it as create_file.php.

  1. <?php
  2. if(ISSET($_POST['create'])){
  3. $content = $_POST['content'];
  4. $file_name = $_POST['filename'];
  5. $path = "files";
  6. $file = fopen($path."/".$file_name.".txt", 'w');
  7. fwrite($file, $content);
  8. fclose($file);
  9. header("location:index.php");
  10. }
  11. ?>

Creating the Main Function

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

  1. <?php
  2. if(ISSET($_REQUEST['file_name'])){
  3. if(unlink('files/'.$_REQUEST['file_name'])){
  4. header('location: index.php');
  5. }
  6. }
  7. ?>

There you have it we successfully created Dynamically Remove File 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.