Koshi
Koshi

Reputation: 143

Showing file icons based on file extension

I'm making a table of uploaded files: file, name, extension (in php with connection to ms sql server).

I would like to add an icon to each of those files, based on their extension. I think there are 3 options to achieve this:

  1. best - load somehow user default icon and somehow connect them with my files
  2. good - load somehow from some webpage list of icon and somehow connect them with my files
  3. bad - put some icons to my folder and load it from that file

I know how to do the third option but I can't just search for 10 icons because there always will be a extension that isn't in my folder (like jpeg is different from jpg).

Could you tell me how can I do some better solution then number 3? It can be in PHP or JS/jQuery, it doesn't matter.

Upvotes: 7

Views: 13642

Answers (4)

Burgi
Burgi

Reputation: 434

Unfortunately I don't think there is an easy way round doing this and option 3 is the only really viable solution.

I have used a similar solution to @MayankPandeyz but using FontAwesome for our icon library and a switch statement to select the file extension. I have found the switch is a little neater to read and easier to maintain.

PHP

function getFileType(string $url):string{
    $filename=explode('.',$url);
    $extension=end($filename);

    switch($extension){
        case 'pdf':
            $type=$extension;
            break;
        case 'docx':
        case 'doc':
            $type='word';
            break;
        case 'xls':
        case 'xlsx':
            $type='excel';
            break;
        case 'mp3':
        case 'ogg':
        case 'wav':
            $type='audio';
            break;
        case 'mp4':
        case 'mov':
            $type='video';
            break;
        case 'zip':
        case '7z':
        case 'rar':
            $type='archive';
            break;
        case 'jpg':
        case 'jpeg':
        case 'png':
            $type='image';
            break;
        default:
            $type='alt';
    }

    return $type;
}

HTML

<?php $url='http://example.com/downloads/file.pdf'; ?>
<div class="card blogLink-bottom download-wrapper">
    <div class="card-img-top img-fluid"> <i class="fas fa-file-<?php echo getFileType($url); ?>"></i></div>
    <div class="card-body blue-bg">
        <h4 class="card-title bold"><?php the_title(); ?></h4>
        <a href="<?php echo $url; ?>" class="btn btn-primary" download><span>Download</span></a>
    </div>
</div>

Here is an example for our output:

Sample image

Upvotes: 4

Darek MST
Darek MST

Reputation: 117

You have to do this by using default icon to unknown types of files.

You may show files for example in table

HTML

<tr>
<td class="fm fm_file">
<a  target="_blank" href="./download.php?f=something">yourfile.jpg</a>
</td>
</tr>

a fragment of my code that does this(fragment of a larger library)

PHP

if ($handle = opendir($directory)) {
            while (false !== ($entry = readdir($handle))) {
                if ($entry != "." && $entry != "..") {
                    if (!is_dir($directory . DIRECTORY_SEPARATOR . $entry)) {
                        $md = rand(0, 9) . substr(md5('download' . $entry), 1, 10) . rand(1000, 9999);
                        $dlink = '<a  target="_blank" href="./download.php?f=' . $entry . '&c=' . $md . '" >' . $entry . '</a>';
                        $editlink='';
                        $a=explode('.', $entry);

                        if(in_array(strtolower(array_pop($a)),array('ini','txt','xml','bin','sql')))
                        $editlink='<a  href="filemanager-edit?dir=' . $subdir.'&f=' . $entry . '&c=' . $md . '" >' . $ledit . '</a>';
                        $filelist.='<tr>' . '<td class="fm fm_file">' . $dlink . '</td>' . '<td class="edit">'.$editlink.'</td>' . '<td class="delete"><a  href="filemanager?action=delete&dir=' . $subdir.'&f=' . $entry . '&c=' . $md . '" >' . $ldelete . '</a></td>' . '</tr>';
                    } else
                        $filelist.='<tr>' . '<td class="fm ft_folder"><a  href="filemanager?action=view&dir=' . $subdir . $entry . '&c=' . md5($entry) . '" >' . $entry . '</a></td>' . '<td class="edit"></td>' . '<td class="edit"><a  href="filemanager?action=view&dir=' . $subdir . $entry . '&c=' . md5($entry) . '" >' . $lchoose . '</a></td>' . '</tr>';
                }
            }
            closedir($handle);
        }

to assign icons need to use js

JavaScript

$('.fm_file').each(function(){
       var name=$(this).find('a').html().split('.').pop();
     $(this).addClass('ft_'+name);
   });

shortcuts to icons files in css

CSS

.fm_file{
    background-image:url(../../images/filemanager/page_white.png);
}

.ft_folder{
    background-image:url(../../images/filemanager/folder.png);
}

.ft_pdf{
    background-image:url(../../images/filemanager/page_white_acrobat.png);
}

.ft_cs{
    background-image:url(../../images/filemanager/page_white_csharp.png);
}

.ft_xls{
    background-image:url(../../images/filemanager/page_white_excel.png);
}

.ft_php{
    background-image:url(../../images/filemanager/page_white_php.png);
}
.ft_dll{
    background-image:url(../../images/filemanager/page_white_dll.png);
}
.ft_exe,.ft_msi{
    background-image:url(../../images/filemanager/page_white_exe.png);
}
.ft_db,.ft_sql{
    background-image:url(../../images/filemanager/page_white_db.png);
}
.ft_png,
.ft_jpg,
.ft_bmp,
.ft_gif{
    background-image:url(../../images/filemanager/page_white_picture.png);
}

.ft_txt,
.ft_js,
.ft_ini,
.ft_bat,
.ft_css{
    background-image:url(../../images/filemanager/page_white_text.png);
}

.ft_htm,
.ft_xml,
.ft_html{
    background-image:url(../../images/filemanager/page_white_code.png);
}

.ft_rar,
.ft_zip{
    background-image:url(../../images/filemanager/page_white_compressed.png);
}

Result:

enter image description here

If someone uses an unknown type is used the image of the class fm_file. You can always make up a collection of icons

Upvotes: 6

Trialsman
Trialsman

Reputation: 359

Since you already have the extension in your database table, the next logical step is to load all the common file extensions into an array along with a reference to the appropriate icon.

Then use in_array to test the extension and load the correct icon.

You can get icon sets for file types in many different styles.

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Try some thing like this:

if(ext == 'jpg' || ext == 'jpeg')
{
    $image_name = 'image.jpg';  
}
if(ext == 'doc' || ext == 'docx')
{
    $image_name = 'doc.jpg';    
}

and make an else for unknown icon

Upvotes: 1

Related Questions