How can PHP be used to read the contents of a text file and display them in a form for editing?
To read the contents of a text file in PHP and display them in a form for editing, you can use the file_get_contents() function to read the file contents, and then output them within a textarea element in an HTML form. You can also use the file_put_contents() function to save the edited contents back to the file.
<?php
$file = 'example.txt';
if(isset($_POST['submit'])) {
$new_content = $_POST['content'];
file_put_contents($file, $new_content);
}
$content = file_get_contents($file);
?>
<form method="post">
<textarea name="content" rows="10" cols="50"><?php echo $content; ?></textarea><br>
<input type="submit" name="submit" value="Save">
</form>
Related Questions
- What are common pitfalls to be aware of when using fwrite() in PHP?
- How can a graphic designer with limited programming experience effectively collaborate with a PHP developer to implement sorting buttons for tables?
- What are the potential performance implications of using HTTP to access a CSV file for data processing in PHP?