Displaying File Icons for Downloads With PHP
I've worked on a few projects where a client had a series of files they wanted to offer as downloads, and the file types were all different. It's nice to show users what type of file they are downloading with an icon that represents that format. To do this, I store the file info in a table that might look something like this:
The $_FILES super global variable gives you access to all of this useful information about your file when it is uploaded, and storing it in the table along with the file name and date posted makes looping through them to display on a file download page convenient and easy. If your file form tag was called "newFile", here's what your SQL statement might look like to store the record in your database:
-
$sql = "INSERT INTO filesTable SET fileTitle='".$_POST['fileTitle']."', fileName='".$_FILES['newFile']['name']."', fileSize='".$_FILES['newFile']['size']."', fileType='".$_FILES['newFile']['type']."', datePosted='".time()."' ";
With the file type stored, when you pull the records out for display, you can use the fileType field to generate an image tag with the
corresponding file type icon. I wrote a simple function to handle this for me that I keep in an external file, and simply pass it the fileType and fileName strings to get the correct image tag. Here's what that looks like:
-
<?
-
function icon($file, $filename){
-
-
// Determine File Type
-
// See list of all possible mime types at http://www.webmaster-toolkit.com/mime-types.shtml
-
switch( $file ){
-
-
case "text/html": $file = "html.gif"; $alt = "HTML File"; break;
-
-
case "text/shtml": $file = "html.gif"; $alt = "HTML File"; break;
-
-
case "image/pjpeg": $file = "jpeg.gif"; $alt = "Progressive Jpeg Image"; break;
-
-
case "image/jpeg":$file = "jpeg.gif"; $alt = "Jpeg Image"; break;
-
-
case "image/png": $file = "png.gif"; $alt = "PNG Image"; break;
-
-
case "image/gif": $file = "gif.gif"; $alt = "GIF Image"; break;
-
-
case "image/tiff": $file = "tiff.gif"; $alt = "Tiff Image"; break;
-
-
case "application/pdf": $file = "pdf.gif"; $alt = "PDF File"; break;
-
-
case "application/word": $file = "word.gif"; $alt = "Word Document"; break;
-
-
case "application/msword": $file = "word.gif"; $alt = "Word Document"; break;
-
-
case "application/powerpoint": $file = "excel.gif"; $alt = "Excel Spreadsheet"; break;
-
-
case "application/excel": $file = "powerpoint.gif"; $alt = "Powerpoint Presentation"; break;
-
-
case "text/plain": $file = "txt.gif"; $alt = "Plain Text File"; break;
-
-
case "application/zip": $file = "zip.gif"; $alt = "Zip Archive"; break;
-
-
case "application/x-stuffit": $file = "sit.gif"; $alt = "Stuffit Archive"; break;
-
-
case "application/x-msdownload": $file = "exe.gif"; $alt = "Executable"; break;
-
-
case "application/octet-stream":
-
// Find Out If File Is PSD
-
$file = "psd.gif"; $alt = "Photoshop Document";
-
$file = "icc.gif"; $alt = "ICC Profile";
-
}else{
-
// Use Generic Icon
-
$fileicon = "doc.gif"; alt="Unknown File Type";
-
}
-
break;
-
-
default: $file = "file.gif"; $alt = "Unknown Tile Type"; break;
-
-
}
-
-
$imgTag = "<img xsrc=\"images/$file\" alt=\"$alt\" />";
-
-
return $imgTag;
-
-
}
-
?>
The function is really just a big switch statement looking at the fileType string. The reason I pass the fileName string as well is because certain files like PSDs, and color correction profiles (ICC), share the same MIME type. Actually there are a lot of file types that share the "application/octet-stream" MIME type, so checking the file's extension can provide a means of determining what kind of file it really is. Note, this is not a sure fire solution if the file does not have an extension or has the wrong one. If the function gets stumped, it returns a generic file icon instead. Here's how you would use it:
You could easily extend the icon function to recognize many other formats just by adding new cases. To see a complete listing of MIME types visit http://www.webmaster-toolkit.com/mime-types.shtml. You can download a good starter set of file icons here: File Icons





It will be better to obtain the icon directly from the system.
In java that can be easily achieved:
//Create a temporary file with the specified extension
file = File.createTempFile("icon", "." + extension);
FileSystemView view = FileSystemView.getFileSystemView();
Icon icon = view.getSystemIcon(file);
//Delete the temporary file
file.delete();
i dont know how to do it in php, but sure it is possible, since the icon is provided by the system.
July 4th, 2007 at 8:54 amRegards,
Nacho
There is a much easier method,
Once you have the extension, do something like:
$ext = // get extension
if( file_exists("./icons/".$ext.".png") ){
$img = "./icons/".$ext.".png";
} else {
$img = "./icons/unkown.png";
}
print "";
September 27th, 2007 at 6:07 pmInteresting thing nacho is mentioning. It would be cool if it was a system icon so the users os would know what to do with the file. I have never seen anything like it in PHP. We are more talking about browser restrictions here.
January 15th, 2008 at 5:07 pm