What is the best way to save text from a textarea into an HTML or PHP file in PHP?

To save text from a textarea into an HTML or PHP file in PHP, you can use the file_put_contents() function to write the text to a file. You can retrieve the text from the textarea using $_POST or $_GET, sanitize it to prevent any malicious code injections, and then write it to the file.

<?php
if(isset($_POST['textarea_content'])){
    $content = $_POST['textarea_content'];
    $sanitized_content = htmlspecialchars($content); // Sanitize the content
    file_put_contents('saved_text.txt', $sanitized_content); // Write the content to a file
    echo 'Text saved successfully!';
}
?>
<form method="post">
    <textarea name="textarea_content"></textarea>
    <input type="submit" value="Save Text">
</form>