What are the potential security risks associated with including external files based on form submissions in PHP?

Including external files based on form submissions in PHP can lead to security risks such as remote code execution, file inclusion vulnerabilities, and potential injection attacks. To mitigate these risks, it is important to validate and sanitize user input before including any external files.

// Validate and sanitize the input before including the external file
$filename = filter_input(INPUT_POST, 'filename', FILTER_SANITIZE_STRING);

// Check if the file exists before including it
if (file_exists($filename)) {
    include $filename;
} else {
    echo 'File not found';
}