What are the potential pitfalls of using file_get_contents to read files into a textarea in PHP?

Using file_get_contents to read files into a textarea in PHP can potentially expose your application to security risks, such as allowing users to read sensitive files on your server. To mitigate this risk, you should sanitize the file path to ensure that only allowed files can be read.

<?php
$file_path = 'path/to/allowed/file.txt';

// Sanitize the file path to ensure it is within the allowed directory
if (strpos($file_path, 'allowed_directory/') === 0) {
    $file_content = file_get_contents($file_path);
    echo '<textarea>' . htmlspecialchars($file_content) . '</textarea>';
} else {
    echo 'Invalid file path.';
}
?>