How can one effectively hide file extensions when displaying images in a PHP script?

To effectively hide file extensions when displaying images in a PHP script, you can use URL rewriting to mask the file extensions. This can help improve the security of your website by not exposing the file types to users. Additionally, you can use a combination of PHP functions like pathinfo() to extract the file extension and basename() to get the file name without the extension.

<?php
// Get the file path from the URL parameter
$file = $_GET['file'];

// Extract the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);

// Get the file name without the extension
$filename = basename($file, '.' . $ext);

// Display the image with the hidden file extension
echo '<img src="' . $filename . '" alt="Image">';
?>