New to our community ?

Discover a world of possibilities! Join us and explore a vibrant community where ideas flourish and connections thrive.

One of Our Valued Members

Thank you for being part of our community. Your presence enriches our shared experiences. Let's continue this journey together!

Home php codes How to edit/update a txt file with php

How to edit/update a txt file with php

0
How to edit/update a txt file with php

Welcome back to shortlearner.com, in this post we will see how to update a txt file with the help of PHP.
So in this post we learn how to fetch data from the file and show in the text area and when user changes in it and hit on the update button file will update.

update txt file php


there is a myfile.txt file in my directory and in the below code i am fetching data from myfile.txt with the help of PHP and showing the data into text area.

Method 1:

in this method we fetch data from PHP file with the help of loop.

<?php
$file = file("myfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
} 
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
?>

Method 2: file_get_contents

In this method we used PHP predefined function file_get_contents.
this function is use for get the contents of a file and store the content into a variable.
if we need to store content into a file than we also use file_put_contents which is works just opposite to file_get_contents funcion.

<?php
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
echo $text = file_get_contents('myfile.txt');
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
?>

after showing the file data into text area we will update the data into file. so when user click on the update button the files will get open again and with the help of write method we writes the data into file and close the file.
here is the complete code.

<?php
if($_POST['Submit']){
$open = fopen("myfile.txt","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "File updated.<br />"; 
echo "File:<br />";
$file = file("myfile.txt");
foreach($file as $text) {
echo $text."<br />";
}
}else{
$file = file("textfile.txt");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"50\" rows=\"10\">";
foreach($file as $text) {
echo $text;
} 
echo "</textarea>";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>

Also Read
How to Install PHP on CentOS.
How to Send Attachment on mail using PHP.

PHP Login Script With Remember me.
Unable to create a directory a wordpress error

How to integrate Razorpay Payment Gateway using PHP.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript

Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

If these article helps you please share it with your developers buddy.
and if you are a web designer and looking for amazing snippets thank click on the below button.

Amazing Snippets

Keep Learning, Keep Coding