What are some common mistakes or misunderstandings when trying to include images in PHP using parameters like $_GET['pic']?

One common mistake when including images in PHP using parameters like $_GET['pic'] is not properly sanitizing the input, which can lead to security vulnerabilities like directory traversal attacks. To solve this, it's important to validate and sanitize the input before using it to include an image.

// Validate and sanitize the input
$pic = isset($_GET['pic']) ? $_GET['pic'] : 'default.jpg';
$pic = basename($pic); // Get the base name of the file

// Include the image using the sanitized input
echo '<img src="images/' . $pic . '" alt="Image">';