How can PHP be used to display different file types with icons on a website?

To display different file types with icons on a website using PHP, you can create an array that maps file extensions to corresponding icon images. Then, when displaying a list of files, you can check the file extension and dynamically insert the appropriate icon image next to each file name.

<?php
// Array mapping file extensions to icon images
$iconMapping = [
    'pdf' => 'pdf-icon.png',
    'doc' => 'doc-icon.png',
    'xls' => 'xls-icon.png',
    // Add more file extensions and corresponding icons as needed
];

// Function to get file extension
function getFileExtension($filename) {
    return pathinfo($filename, PATHINFO_EXTENSION);
}

// Sample list of files
$files = ['document1.pdf', 'spreadsheet.xls', 'presentation.ppt'];

// Display list of files with icons
foreach ($files as $file) {
    $fileExt = getFileExtension($file);
    $icon = isset($iconMapping[$fileExt]) ? $iconMapping[$fileExt] : 'default-icon.png';
    echo '<img src="icons/' . $icon . '" alt="' . $fileExt . ' icon">' . $file . '<br>';
}
?>