When a file/image is uploaded via Drupal, they go into a common directory specified by the administrator.

However, is there a way to specify where the files will be uploaded, according to their node ID? For example, say, a person creates a page and Drupal assigns it an ID of 1234; thus, the uploaded files will go into {specified_location}/1234_files/.

up vote 4 down vote accepted

You can use the File (Field) Paths module in combination with the Token module for this.

  1. Enable module
  2. Go to admin/structure/types/manage/your_content_type/fields/your_file_field
  3. Make sure Enable File (Field) Paths? is enabled.
  4. Under File (Field) Path settings you can now use e.g. myfiles/[node:nid]

Try the File (Field) Paths module with Token module

Token

Tokens are small bits of text that can be placed into larger documents via simple placeholders, like %site-name or [user]. The Token module provides a central API for modules to use these tokens, and expose their own token values.

File (Field) Paths

The File (Field) Paths module extends the default functionality of Drupal's core Upload module, the FileField module and many other File Upload modules by adding the ability to use node tokens in destination paths and filenames.

On the file field edit page you can use Replacement Patterns for File Directory field

  • Op asks for [nid]. I think token does not support [nid] token out of the box. – undersound Jul 6 '14 at 5:47
  • @undersound Thanks I updated my answer to make it valid – Krishna Mohan Jul 6 '14 at 9:14

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.