Home / Comment permalink

Preventing Drupal Headaches! The file field

In our structured Drupal training program, we teach the "right way" to do things. We listen to our support team of client advisors who see all the permutations of "you're doing it wrong" in Drupal, and make sure we're leading learners in the right direction. When there's something particularly essential we use words like "Important" and set off warnings with a little exclamation mark icon. What we don't do is surround particular instruction in skulls and cross-bones and "Ye shall not pass" signs explaining the hells that will befall you if you don't do something a certain way. Because everything in Drupal is optional, and up to you.

Take, for example, the file field "File directory" path settings. If you didn't know it could lead you to potential disaster, you might think this "optional" setting is something you could just overlook. Many clients add file fields, and never complete this configuration. With multiple content types, files on taxonomy terms or other entities, it means ALL the uploaded files across the site are going into the same directory.

File field path settings

In fact, we encounter this problem while supporting some of the biggest and busiest websites with our hosting business. "How many files can you store in a directory" depends on your server software and configuration. Like the proverbial "clowns in a car" trick, you don't want to attempt to reach that upper limit. While the technical upper limits may be high, you can notice performance implications with 1000 or 100,000 files. This could be when you try to load a directory, or even display an image from a directory. You may never think you'll get up to that many files, but if you have users generating content, you can get surprised.

Especially if you haven't let Drupal handle the file referencing and management, remediating the problem is painfully slow. For example, adding inline references to images on your server, instead of using modules like Media which use tokens to place images inline. My colleague, Devin Carlson, (who is very knowledgeable about the file system in Drupal 7) explained it took him 1-2 weeks to fix a similar problem on a client site. He used the Migrate module, wrote custom scripts and tested over and over again to ensure all the files were moved and all internal inline references were updated.

How to add file upload fields to Drupal the right way

In terms of file path management, you can do three things:

  1. Even if you don't install another module you can add a prefix to each instance of a file field. For example, add the entity name (content type, taxonomy term name).
  2. Use the Token module and you can use the current date as a dynamic configuration.
  3. Use the File (Field) Paths module for ALL the options. With this module, you can access all node tokens, such as the date when the node was created or the author. The File field paths module also lets you rename the files.

Browse for available tokens

If you're new to Drupal, I've got a more complete tutorial for you. In my live online walkthrough last week, I walked through all the basics of adding file fields, how to configure private or public download methods, and other essentials. I mentioned the very important modules you should already be using, and I talked about additional modules you can use to improve the process, and some future friendly tips about what is being worked on for media and file management in Drupal 8. You can watch it here:

You can also flip through the slides here:

Some good news for Drupal 8!

I'm guessing that Dries's tweet about this issue was sparked most likely by a client issue on our own servers.

The good thing is, it prompted a Twitter discussion and an issue now for Drupal 8 to set these defaults to something safe and reasonable. See Files should automatically be uploaded to directories based on a token for more details and to weigh in on the issue.

Come to our live and online training sessions

Check out our training events calendar for public training. In Site Building with Drupal 7, you can learn all the nuts and bolts of building flexible future friendly websites. We'll be adding more dates soon, but please tell us where you'd like to see training next.

More upcoming webinars to prevent Drupal headaches like this!

My live online walk throughs are on Wednesdays. 10 am in California, 1pm Boston time, 6pm in London. It's at 3am Tokyo time, and 5am in Sydney, so if you don't want to waking up at odd hours, you can sign up anyway, and you'll get a notification when the recordings and the slides are available!


Posted on by Jeff Mahoney.

Hi, the link in the first paragraph is broken. It points to : https://www.a cquia.com/training.acquia.com/events

Jeff at Thought Matrix

Posted on by MXT (not verified).

File Fields Path works with file fields, but doesn't works on File entity itself.

But there's a module that resolve this: https://drupal.org/project/ fe_paths

More info on: https://drupal.org/node/1360576

Posted on by heather.

Thanks Jeff and MXT!

Posted on by S L.

I am using Date tokens to image fields.
However,I need to refresh the page so as image to appear.
How can I fix that?

Posted on by Freddy (not verified).

What about user profile pictures? Could they become problematic if a site gets enough users?

Posted on by Khaled_webdev (not verified).

I think using token with custom date is better to give a better structure by date


Thanks alot for thsi great article

Posted on by Avgoustinos (not verified).

hi, i have 2 fields one cv and the other cover, i would like to save both in one directory, how can i do that?

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Filtered HTML

  • Use [acphone_sales], [acphone_sales_text], [acphone_support], [acphone_international], [acphone_devcloud], [acphone_extra1] and [acphone_extra2] as placeholders for Acquia phone numbers. Add class "acquia-phones-link" to wrapper element to make number a link.
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <h4> <h5> <h2> <img>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.