What are the potential security risks associated with allowing users to input content directly into a .txt file on a PHP website?

Allowing users to input content directly into a .txt file on a PHP website can pose security risks such as code injection, file inclusion attacks, and unauthorized access to sensitive files on the server. To mitigate these risks, it is important to sanitize and validate user input before writing it to the .txt file.

<?php
// Sanitize and validate user input
$user_input = filter_var($_POST['user_input'], FILTER_SANITIZE_STRING);

// Write sanitized input to a .txt file
$file = fopen("user_input.txt", "w");
fwrite($file, $user_input);
fclose($file);
?>