In this tutorial we will create a
Dynamically Create Text File using PHP. This code can create a text file with content dynamically when user submit the form inputs. The code use PHP
POST to launch a specific method that create a text using these special php functions
fopen() to open an existing file and if not exist it will create a new file,
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 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
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";
while(($file = readdir($dir)) !== FALSE){
if($file == "." || $file == "..")
continue;
?>
<tr>
<td><?php echo $file?></td>
</tr>
<?php
}
?>
</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');
}
?>
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!