Skip to content

Instantly share code, notes, and snippets.

@guedressel
Last active October 13, 2022 09:30
Show Gist options
  • 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";
}
@colemanw
Copy link

Here is my revision that supports more file types: https://gist.github.com/colemanw/9c9a12aae16a4bfe2678de86b661d922

@Kaabi
Copy link

Kaabi commented Jan 14, 2019

I updated your function to current Fontawesome 5.6 icons

 function FontAwesomeIcon($mime_type)
    {
        // List of official MIME Types: http://www.iana.org/assignments/media-types/media-types.xhtml
        static $font_awesome_file_icon_classes = [
            // Images
            'image'                                                                     => 'fa-file-image',
            // Audio
            'audio'                                                                     => 'fa-file-audio',
            // Video
            'video'                                                                     => 'fa-file-video',
            // Documents
            'application/pdf'                                                           => 'fa-file-pdf',
            'application/msword'                                                        => 'fa-file-word',
            'application/vnd.ms-word'                                                   => 'fa-file-word',
            'application/vnd.oasis.opendocument.text'                                   => 'fa-file-word',
            'application/vnd.openxmlformats-officedocument.wordprocessingml'            => 'fa-file-word',
            'application/vnd.ms-excel'                                                  => 'fa-file-excel',
            'application/vnd.openxmlformats-officedocument.spreadsheetml'               => 'fa-file-excel',
            'application/vnd.oasis.opendocument.spreadsheet'                            => 'fa-file-excel',
            'application/vnd.ms-powerpoint'                                             => 'fa-file-powerpoint',
            'application/vnd.openxmlformats-officedocument.presentationml'              => 'ffa-file-powerpoint',
            'application/vnd.oasis.opendocument.presentation'                           => 'fa-file-powerpoint',
            'text/plain'                                                                => 'fa-file-alt',
            'text/html'                                                                 => 'fa-file-code',
            'application/json'                                                          => 'fa-file-code',
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document'   => 'fa-file-word',
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'         => 'fa-file-excel',
            'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'fa-file-powerpoint',
            // Archives
            'application/gzip'                                                          => 'fa-file-archive',
            'application/zip'                                                           => 'fa-file-archive',
            'application/x-zip-compressed'                                              => 'fa-file-archive',
            // Misc
            'application/octet-stream'                                                  => 'fa-file-archive',
        ];

        if (isset($font_awesome_file_icon_classes[$mime_type]))
            return $font_awesome_file_icon_classes[$mime_type];

        $mime_group = explode('/', $mime_type, 2)[0];
        return (isset($font_awesome_file_icon_classes[$mime_group])) ? $font_awesome_file_icon_classes[$mime_group] : 'fa-file';
    }

@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