Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 512
Koshi Avatar asked Aug 01 '16 12:08

Koshi


1 Answers

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

like image 77
Darek MST Avatar answered Sep 17 '22 22:09

Darek MST