Skip to content

Instantly share code, notes, and snippets.

@guedressel
Last active October 13, 2022 09:30
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save guedressel/0daa170c0fde65ce5551 to your computer and use it in GitHub Desktop.
Save guedressel/0daa170c0fde65ce5551 to your computer and use it in GitHub Desktop.
Font Awesome File Icons: Mapping MIME Types to correct icon classes
<?php
/**
* Get font awesome file icon class for specific MIME Type
* @see https://gist.github.com/guedressel/0daa170c0fde65ce5551
*
*/
function font_awesome_file_icon_class( $mime_type ) {
// List of official MIME Types: http://www.iana.org/assignments/media-types/media-types.xhtml
static $font_awesome_file_icon_classes = array(
// Images
'image' => 'fa-file-image-o',
// Audio
'audio' => 'fa-file-audio-o',
// Video
'video' => 'fa-file-video-o',
// Documents
'application/pdf' => 'fa-file-pdf-o',
'text/plain' => 'fa-file-text-o',
'text/html' => 'fa-file-code-o',
'application/json' => 'fa-file-code-o',
// Archives
'application/gzip' => 'fa-file-archive-o',
'application/zip' => 'fa-file-archive-o',
// Misc
'application/octet-stream' => 'fa-file-o',
);
if (isset($font_awesome_file_icon_classes[ $mime_type ])) {
return $font_awesome_file_icon_classes[ $mime_type ];
}
$mime_parts = explode('/', $mime_type, 2);
$mime_group = $mime_parts[0];
if (isset($font_awesome_file_icon_classes[ $mime_group ])) {
return $font_awesome_file_icon_classes[ $mime_group ];
}
return "fa-file-o";
}
@DigitalLeaves
Copy link

Excellent work, thank you. I used your code as inspiration to build a gist to map filenames (using the extension) to Font Awesome classes.
https://gist.github.com/DigitalLeaves/414365b686f7991af9d4fb9bdcc4cc4e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment