File I/O to textarea in PHP
Solution 1
This is simpler than you think. You shouldn't be outputting the <br>
tags as the textarea already contains the entered newline characters (\r\n
or \n
). You don't have to read the file like that, if you read it this way, you never have to worry about the character contents.
Change:
$fp = fopen($file, "r");
while(!feof($fp)) {
$data = fgets($fp, filesize($file));
echo "$data <br>";
}
fclose($fp);
to:
echo file_get_contents( $file);
Problem solved.
Solution 2
This is happening because while writing the contents to the text area, you're putting a <br>
at the end of each line. But in a textarea line breaks are noted by "\n"
. When you're saving your existing text, the next time the line breaks are replaced with more <br>
.
While printing out the content on a public page, keep the
. But in the editing page, remove the br.
Here's what I would have done with the PHP code:
<?php
define("FILE_NAME", "tester.txt");
function Read()
{
echo @file_get_contents(FILE_NAME);
}
;
function Write()
{
$data = $_POST["tekst"];
@file_put_contents(FILE_NAME, $data);
}
?>
<?php
if ($_POST["submit_check"])
{
Write();
}
?>
Latze
Updated on September 23, 2022Comments
-
Latze over 1 year
My friend and I have a little spare time home page together. He's not a programmer, and in order for him to be able to change some text on the front page, I created a php-script that
1) Reads data from file "tester.txt" (this is the text that should go on the front page)
2) Prints this text to a textarea, where you can edit the text and submit it again
3) Writes the edited text to the same file, "tester.txt"The two functions Read(); and Write(); look like this
function Read() { $file = "tester.txt"; $fp = fopen($file, "r"); while(!feof($fp)) { $data = fgets($fp, filesize($file)); echo "$data <br>"; } fclose($fp); } function Write() { $file = "tester.txt"; $fp = fopen($file, "w"); $data = $_POST["tekst"]; fwrite($fp, $data); fclose($fp); }
The only problem I have is that when the text is printed to a text area the line returns are written as
<br>
- and I don't really want it to do that, because when you edit some of the code and rewrites it, another layer of<br>
's appear. Here's a screenshot to illustrate:Is there any workaround to this?
Thanks!
If you need the rest of the code, here it is:
<html> <head> <title>Updater</title> </head> <body> <?php function Read() { $file = "tester.txt"; $fp = fopen($file, "r"); while(!feof($fp)) { $data = fgets($fp, filesize($file)); echo "$data <br>"; } fclose($fp); } function Write() { $file = "tester.txt"; $fp = fopen($file, "w"); $data = $_POST["tekst"]; fwrite($fp, $data); fclose($fp); } ?> <?php if ($_POST["submit_check"]){ Write(); }; ?> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <textarea width="400px" height="400px" name="tekst"><?php Read(); ?></textarea><br> <input type="submit" name="submit" value="Update text"> <input type="hidden" name="submit_check" value="1"> </form> <?php if ($_POST["submit_check"]){ echo 'Text updated'; }; ?> </body> </html>