What is the best way to replace the content of a file with the content of a textarea in PHP?

To replace the content of a file with the content of a textarea in PHP, you can read the content of the textarea using $_POST, then write this content to the file using file_put_contents() function. Make sure to sanitize and validate the input before writing it to the file to prevent any security vulnerabilities.

<?php
if(isset($_POST['textarea_content'])){
    $content = $_POST['textarea_content'];
    $file = 'file.txt';
    
    // Sanitize and validate the input if needed
    
    file_put_contents($file, $content);
    echo "Content replaced successfully.";
}
?>