What are the potential security risks of executing PHP code from a file input?

Executing PHP code from a file input can pose a significant security risk as it allows for arbitrary code execution, potentially leading to server compromise or data breaches. To mitigate this risk, it is important to validate and sanitize user input before executing it as PHP code.

// Validate and sanitize file input before executing as PHP code
$file = $_FILES['file']['tmp_name'];

if (pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION) !== 'php') {
    die('Invalid file type. Only PHP files are allowed.');
}

if (is_uploaded_file($file)) {
    include $file;
} else {
    die('Error uploading file.');
}