PHP - Dynamically Remove File

Submitted by: 
Visitors have accessed this post 184 times.

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

And, this is the link for the bootstrap that i used for the layout design

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

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.