What potential pitfalls should be considered when including files in PHP projects?

One potential pitfall when including files in PHP projects is the risk of including files from untrusted sources, which could lead to security vulnerabilities such as code injection or file manipulation. To mitigate this risk, it is important to validate user input and use proper file path sanitization techniques before including any files in PHP.

// Example of validating and sanitizing file path before including
$filename = $_GET['file']; // Assuming user input is provided via GET parameter
$allowed_files = ['file1.php', 'file2.php']; // List of allowed files

if (in_array($filename, $allowed_files)) {
    $filepath = 'path/to/files/' . $filename;
    include($filepath);
} else {
    echo "Invalid file requested.";
}