Dynamically Create A Text File In PHP | CampCodes

Dynamically Create a Text File in PHP

June 15, 2020
PHP
create a text file in php

In this tutorial, we will create a Dynamically Create Text File using PHP. This code can generate a text file with content dynamically when the user submits the form inputs. The system uses PHP POST to launch a specific method that creates a text utilizing these special PHP functions fopen() to open an existing file, and if not exist it will create a new record, fwrite() insert some content in the text file, and lastly fclose() safely close the connection between the opened file. This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language and interpreter, which is mainly used on any web server, including xamp, wamp, etc. It is being used to any popular websites, and it has a modern technology that can easily be used by the next generation.

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 structure and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Dynamically Create Text File</h3>
        <hr style="border-top:1px dottec #ccc;"/>
        <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>
                </form>
            </div>
        </div>
        <div class="col-md-8">
            <table class="table table-bordered">
                <thead class="alert-info">
                    <tr>
                        <th>File Name</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $full_path = "files";
 
                        $dir = opendir($full_path);
 
                        while(($file = readdir($dir)) !== FALSE){
                            if($file == "." || $file == "..")
 
                            continue;
                        ?>
                            <tr>
                                <td><?php echo $file?></td>
                            </tr>	
                    <?php
 
                        }
 
                        closedir($dir);
                    ?>
 
                </tbody>
            </table>
        </div>
    </div>
 
 
</body>
</html>

Creating the Main Function

This code contains the main function of the application. This code will create 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 string.php

<?php
    if(ISSET($_POST['create'])){
        $content = $_POST['content'];
        $file_name = $_POST['filename'];
        $path = "files";
        $file = fopen($path."/".$file_name.".txt", 'w');
        fwrite($file, $content);
        fclose($file);
        header("location:index.php");
    }
?>

There you have it we successfully created Dynamically Create Text 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!

Icon

PHP - Dynamically Create Text File Source Code 337.44 KB 1174 downloads

Please go back to the description page. Just Click the Red Download Button on the...

Leave a Reply

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