Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

File icon giver/getter/whatever (See related posts)

This code uses acts_as_attachment (that's the attachment.filename) but you can of course replace it with anything.
It gets the file extension then looks up that extension in a dir full of files named extension.png
if none is found, unknown.png is used


<%
extension = File.extname(attachment.filename).gsub(/\./, "")
# get a file icon
icon_path = nil
Dir.entries("#{RAILS_ROOT}/public/images/filetypes").each do |entry|
	entry_extension = File.extname(entry)
	if entry.gsub(entry_extension, "") === extension
		icon_path = "/images/filetypes/" + entry
		break
	end
end
icon_path = "/images/filetypes/unknown.png" if icon_path.nil?
 %>
<%= image_tag icon_path %>
<span class="attachment_name"><%= attachment.filename %></span><br />

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts