Filter Drupal Content Based on File Type

file filter

One of our members asked an interesting question about Views.

They had a file field on their user profiles. In that field, the user could upload an image, an audio file, or link to a YouTube video. So far, so good. However, in Views, they only wanted to show that field if it contained a video.

Here’s the solution to that problem. We’re going to show you how to filter Drupal content based on the type of file that’s attached to it.

First, you’ll need a content type with a file field:


Make sure that your settings for that field allow users to upload multiple different types of file:


Now, go to Structure > Views. We’re going to create a View that shows only content with image files attached. At this point, we don’t need to do anything different. Just make sure to choose the content type with files attached:


Let’s add the field field so that it’s visible on the page:


Now, we can start to create the filter that will select on content with a particular type of attachment.

  • Under Advanced, click Add next to Relationships.
  • Search for your file field. This needs to be the option with the fid (file ID number) attached:
  • Click Apply twice.
  • Click Add next to Filter Criteria.
  • Search for “File: Mime type”. In this case, mime means “Internet Media Type“:
  • Choose “Starts with”.
  • For the Value, you can enter “image”, “audio” or the choice that you need. Harvard has a complete list of which file types correspond to which media types.

Save that Filter Criteria and your view should be complete! In this example, you would only see content that has an image attached.


  • Steve Burge

    Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. Steve's work straddles the line between teaching and web development.

0 0 votes
Article Rating
Notify of

Newest Most Voted
Inline Feedbacks
View all comments
9 years ago

Great tutorial. Is there a way to add an exposed filter for file extensions? That way a user could narrow down between .gif or .jpeg

8 years ago

Love that aspect of reusable data in Drupal/Views.

(In Joomla and WP it is the moment where you search and install a big 3rd-party extension 😉 )

Would love your thoughts, please comment.x