How can PHP be used to load, edit, and update a .txt file in a textarea?
To load, edit, and update a .txt file in a textarea using PHP, you can use file_get_contents() to load the content of the file into the textarea, allow the user to edit the content, and then use file_put_contents() to update the file with the new content.
<?php
$file = 'example.txt';
if(isset($_POST['content'])) {
$content = $_POST['content'];
file_put_contents($file, $content);
}
$text = file_get_contents($file);
?>
<form method="post">
<textarea name="content"><?php echo $text; ?></textarea>
<input type="submit" value="Save">
</form>
Related Questions
- Are there performance differences between using curly braces and alternative syntax in PHP templates, and what factors should be considered when choosing between them?
- What potential security risks are involved in allowing external file access in PHP?
- How can preg_match_all be used to extract all strings within double quotes in PHP, considering escaped double quotes within the string?