Dynamically Remove A File In PHP | CampCodes

Dynamically Remove a File in PHP

June 6, 2020
unlink a file php

In this tutorial, we will create a Dynamically Remove File using PHP. This code will dynamically remove a file when the user clicks the button within the range of the table row. This code uses a simple href or hypertext reference to call a script within the server that sends the file data, which removes 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 manages a database server to handle a bulk of data per transaction. It describes as an advanced technology that manages both server and control-block of your machine.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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 a copy of the way and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Dynamically Remove File</h3>
        <hr style="border-top:1px dottec #ccc;"/>
        <div class="col-md-8">
            <table class="table table-bordered">
                <thead class="alert-info">
                        <th>File Name</th>
                        $full_path = "files";
                        $dir = opendir($full_path);
                        while(($file = readdir($dir)) !== FALSE){
                            if($file == "." || $file == "..")
                                <td><?php echo $file?></td>
                                <td><a class="btn btn-danger btn-sm" href="delete_file.php?file_name=<?php echo $file?>">Delete</a></td>
        <div class="col-md-4">
            <div class="form-inline">
                <form method="POST" action="create_file.php">
                    <label style="font-size:18px;">Filename:</label>
                    <input type="text" name="filename" class="form-control" required="required"/>
                    <br /><br />
                    <label>Enter a Text</label>
                    <textarea name="content" style="width:100%; height:100px; resize:none;"></textarea>
                    <br /><br />
                    <center><button class="btn btn-primary" name="create">Create File</button></center>

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.

        $content = $_POST['content'];
        $file_name = $_POST['filename'];
        $path = "files";
        $file = fopen($path."/".$file_name.".txt", 'w');
        fwrite($file, $content);

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.

            header('location: index.php');

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!

Leave a Reply

Your email address will not be published. Required fields are marked *