Determine file extension of file value field in flexinode and display file type or icon
Last modified: October 5, 2006 - 13:46
When displaying links to files, if you have a mixed blend of PDFs, DOC's, JPGs, etc, it is good practice to tell your site visitor what type of file he is about to click. This snippet will determine the file type based on the file's extension. Specifically, it's to be used in a flexinode theme, but I imagine it can be used anywhere:
<?php
// file type by extension
$bjmfp = $node->flexinode_16->filepath;
$bjmft = substr($bjmfp, strrpos($bjmfp, ".")+1);
$bjmft = strtoupper($bjmft);
print "[$bjmft]";
?>In the code above, my file field is flexinode_16, and you can see how to get to its propery "filepath." A substring of one position past the last occurrence of "." gives the file extension if there is one. This example outputs
[PDF] for PDFs, but you could easily run a check for PDF and display the typical Acrobat icon instead.

Howto icon images to uploaded files
Thank you, thats what I needed. I added some icons to it, like you mentioned.
kr, Robert
http://easytouch.at
Tested with Drupal 4.7.2
# vi modules/upload.module
// $Id: upload.module,v 1.108 2006/06/11 23:58:55 drumm Exp $
/**
* Displays file attachments in table
*/
function theme_upload_attachments($files) {
/// 55172 ///////////////////////////////////////////////////////////////
// 2006-06-29 robert
// Icons for filetypes
$header = array(t(''), t('Attachment'), t('Size')); //55172
$rows = array();
foreach ($files as $file) {
if ($file->list) {
/*-----------------------------------------------*/
// 55172
// file type by extension
$bjmfp = $file->filepath;
$bjmft = substr($bjmfp, strrpos($bjmfp, ".")+1);
$bjmft = strtoupper($bjmft);
$icon = 'unknown.gif';
if ($bjmft=='BAT'){$icon = 'bat.gif';}
if ($bjmft=='BMP'){$icon = 'img.gif';}
if ($bjmft=='EXE'){$icon = 'exe.gif';}
if ($bjmft=='GIF'){$icon = 'img.gif';}
if ($bjmft=='JPG'){$icon = 'img.gif';}
if ($bjmft=='PNG'){$icon = 'img.gif';}
if ($bjmft=='TIF'){$icon = 'img.gif';}
if ($bjmft=='TIFF'){$icon = 'img.gif';}
if ($bjmft=='DOC'){$icon = 'doc.gif';}
if ($bjmft=='XLS'){$icon = 'xls.gif';}
if ($bjmft=='PPT'){$icon = 'ppt.gif';}
if ($bjmft=='PDF'){$icon = 'pdf.gif';}
if ($bjmft=='GZ'){$icon = 'zip.gif';}
if ($bjmft=='TAR'){$icon = 'zip.gif';}
if ($bjmft=='RAR'){$icon = 'zip.gif';}
if ($bjmft=='TXT'){$icon = 'txt.gif';}
if ($bjmft=='ZIP'){$icon = 'zip.gif';}
$filetype = '<img src="/itil/files/'.$icon.'">';
/*-----------------------------------------------*/
$href = check_url(($file->fid ? file_create_url($file->filepath) : url(file_create_filename($file->filename, file_create_path()))));
$text = check_plain($file->description ? $file->description : $file->filename);
$rows[] = array($filetype, l($text, $href), format_size($file->filesize)); //55172
/// 55172 ////////////////////////////////////////////////////////////////
}
getting the extension...
I tend to prefer
$ext = array_pop(explode('.',$filename));
Simpler code
I use a much simpler variant of this code written for me by Nate Norrish - It checks a directory called images/icons/ for a .png file with the same filename as the uploaded file. i.e: if an uploaded file is whatever.zip, it'll check for zip.png. If it can't find one, it'll just show unknown.png.
Put the following code into your template.php:
<?php
function file_link($filename)
{
$ext = strtolower(substr($filename, strrpos($filename, ".")+1));
$icon_path = "images/icons/";
$icon = (file_exists($icon_path . "$ext.png") ? $icon_path . "$ext.png" : $icon_path . "unknown.png");
return '<a href="' . file_create_url ($filename) . '"><img src="' . $icon .'" border="0" width="48" /><BR />[' . strtoupper($ext) . ']</a>';
}
?>
and then summon it from your .tpl.php file using
<?phpprint file_link($node->flexinode_7->filepath);
?>
If you're interested, here's a bunch of prefab icons I built for my site here
$path_info =
$path_info = pathinfo($file_path);
$file_ext = $path_info['extension'];