File Icons

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ast3r3x
Forum Commoner
Posts: 95
Joined: Thu Aug 19, 2004 8:36 pm

File Icons

Post by ast3r3x »

Is there anyway to get a file's icon with PHP? I'm making a file browser thing for my website and I'd like icons to be able to quickly identify a file type, but didn't know if this was possible. If not it's ok, I'll just make an image for each file extension if it's not possible.

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

image based on file extension is how most work, that I've seen.. even apache's does that.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Code: Select all

function getMimeType($filename)
{
    $filename = escapeshellarg($filename);

    if (function_exists('mime_content_type')) {
        return mime_content_type($filename);
    }

    return trim(current(explode(';', trim(shell_exec("file -bi {$filename}")))));
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Or if you are a "happy" window user and only care about the extension (i know it's not smart)

Code: Select all

/**
 * Given a file extension, this will come up with the filename of the
 * icon to represent the filetype.
 * @param string $ext The file extension to find the icon for
 * @return string The appropriate icon depending on the extension
 */
function find_icon($ext)
{
    if ($ext == '')
    {
	   return 'unknown.png';
    }

    $icon_types = array(
        'binary' => array('bat', 'bin', 'com', 'dmg', 'dms', 'exe', 'msi', 'msp', 'pif', 'pyd', 'scr', 'so'),
        'binhex' => array('hqx'),
        'cd' => array('bwi', 'bws', 'bwt', 'ccd', 'cdi', 'cue', 'img', 'iso', 'mdf', 'mds', 'nrg', 'nri', 'sub', 'vcd'),
        'comp' => array('cfg', 'conf', 'inf', 'ini', 'log', 'nfo', 'reg'),
        'compressed' => array('7z', 'a', 'ace', 'ain', 'alz', 'amg', 'arc', 'ari', 'arj', 'bh', 'bz', 'bz2', 'cab', 'deb', 'dz', 'gz','io', 'ish', 'lha', 'lzh', 'lzs', 'lzw', 'lzx', 'msx', 'pak', 'rar', 'rpm', 'sar', 'sea', 'sit', 'taz', 'tbz', 'tbz2', 'tgz', 'tz', 'tzb', 'uc2', 'xxe', 'yz', 'z', 'zip', 'zoo'),
        'dll' => array('386', 'db', 'dll', 'ocx', 'sdb', 'vxd'),
        'doc' => array('abw', 'ans', 'chm', 'cwk', 'dif', 'doc', 'dot', 'mcw', 'msw', 'pdb', 'psw', 'rtf', 'rtx', 'sdw', 'stw', 'sxw', 'vor', 'wk4', 'wkb', 'wpd', 'wps', 'wpw', 'wri', 'wsd'),
        'image' => array('adc', 'art', 'bmp', 'cgm', 'dib', 'gif', 'ico', 'ief', 'jfif', 'jif', 'jp2', 'jpc', 'jpe', 'jpeg', 'jpg', 'jpx', 'mng', 'pcx', 'png', 'psd', 'psp', 'swc', 'sxd', 'tga', 'tif', 'tiff', 'wmf', 'wpg', 'xcf', 'xif', 'yuv'),
        'java' => array('class', 'jar', 'jav', 'java', 'jtk'),
        'js' => array('ebs', 'js', 'jse', 'vbe', 'vbs', 'wsc', 'wsf', 'wsh'),
        'key' => array('aex', 'asc', 'gpg', 'key', 'pgp', 'ppk'),
        'mov' => array('amc', 'dv', 'm4v', 'mac', 'mov', 'mp4v', 'mpg4', 'pct', 'pic', 'pict', 'pnt', 'pntg', 'qpx', 'qt', 'qti', 'qtif', 'qtl', 'qtp', 'qts', 'qtx'),
        'movie' => array('asf', 'asx', 'avi', 'div', 'divx', 'dvi', 'm1v','m2v', 'mkv', 'movie', 'mp2v', 'mpa', 'mpe', 'mpeg', 'mpg','mps', 'mpv', 'mpv2', 'ogm', 'ram', 'rmvb', 'rnx', 'rp', 'rv','vivo', 'vob', 'wmv', 'xvid'),
        'pdf' => array('edn', 'fdf', 'pdf', 'pdp', 'pdx'),
        'php' => array('inc', 'php', 'php3', 'php4', 'php5', 'phps','phtml'),
        'ppt' => array('emf', 'pot', 'ppa', 'pps', 'ppt', 'sda', 'sdd','shw', 'sti', 'sxi'),
        'ps' => array('ai', 'eps', 'ps'),
        'sound' => array('aac', 'ac3', 'aif', 'aifc', 'aiff', 'ape', 'apl','au', 'ay', 'bonk', 'cda', 'cdda', 'cpc', 'fla', 'flac','gbs', 'gym', 'hes', 'iff', 'it', 'itz', 'kar', 'kss', 'la','lpac', 'lqt', 'm4a', 'm4p', 'mdz', 'mid', 'midi', 'mka','mo3', 'mod', 'mp+', 'mp1', 'mp2', 'mp3', 'mp4', 'mpc','mpga', 'mpm', 'mpp', 'nsf', 'oda', 'ofr', 'ogg', 'pac', 'pce','pcm', 'psf', 'psf2', 'ra', 'rm', 'rmi', 'rmjb', 'rmm', 'sb','shn', 'sid', 'snd', 'spc', 'spx', 'svx', 'tfm', 'tfmx','voc', 'vox', 'vqf', 'wav', 'wave', 'wma', 'wv', 'wvx', 'xa','xm', 'xmz'),
        'tar' => array('gtar', 'tar'),
        'text' => array('c', 'cc', 'cp', 'cpp', 'cxx', 'diff', 'h', 'hpp','hxx', 'm3u', 'md5', 'patch', 'pls', 'py', 'sfv', 'sh','txt'),
        'uu' => array('uu', 'uud', 'uue'),
        'web' => array('asa', 'asp', 'aspx', 'cfm', 'cgi', 'css', 'dhtml','dtd', 'grxml', 'htc', 'htm', 'html', 'htt', 'htx', 'jsp', 'lnk','mathml', 'mht', 'mhtml', 'perl', 'pl', 'plg', 'rss', 'shtm','shtml', 'stm', 'swf', 'tpl', 'wbxml', 'xht', 'xhtml', 'xml','xsl', 'xslt', 'xul'),
        'xls' => array('csv', 'dbf', 'prn', 'pxl', 'sdc', 'slk', 'stc', 'sxc','xla', 'xlb', 'xlc', 'xld', 'xlr', 'xls', 'xlt', 'xlw')
    );
    foreach ($icon_types as $png_name => $exts)
    {
	   if (in_array($ext, $exts))
	   {
            return "{$png_name}.png";
	   }
    }
    return 'unknown.png';
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

8O someone had some extra time on their hands. :)
Post Reply