2

This question already has an answer here:

I added an Image Field to my Node content type. Now, when I upload an image "image1.jpg" that is attached to a node mysite.com/note/123, I want the uploaded image's path to be mysite.com/node/123/image1.jpg. How do I achieve this? I've tried to change the subfolder of the image with a token. I used the token [current-page:path], but that doesn't return the node where I'm attaching to, but the last page that was visited. (Which might be the node edit form or something completely different with ajax, say)

marked as duplicate by mpdonadio Aug 8 '15 at 23:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4

Seems you are looking for a File (Field) Paths module:

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.

In simple terms, File (Field) Paths allows you to automatically sort and rename your uploaded files using token based replacement patterns to maintain a nice clean filesystem.

Be aware that it's not yet stable. Test intensively before using in production environment. On the other hand, I don't know anything better and I know for a fact it may work all right.

  • That's a really useful module! I can do half of the job I want to do, that is, I can make it upload the image to mysite.com/sites/default/files/node/123/image1.jpg. Is there any reasonable way to get rid of the sites/default/files? (Or do I have to cripple my server for that?) – Turion Sep 3 '13 at 12:41
  • I achieved this by setting the file field path to [node:url:path]. – Turion Sep 3 '13 at 12:42
  • 2
    You shouldn't really write files outside /sites/default/files - for security reasons it's better not to allow writing in random places. But you can use mod rewrite to make Apache act as if that part of URL wasn't there, and preprocess function to remove it from links. I'm sure I've seen it asked here already. – Mołot Sep 3 '13 at 12:45
  • Ah, alright, makes sense. Goodbye to the idea that it might be possible to insert images into the body using relative paths... – Turion Sep 3 '13 at 12:57
  • 1
    @Turion it still may be possible all right, see this question – Mołot Sep 3 '13 at 13:11
0

If you wanted a programmatic solution, you could use the setting array when creating image field instance:

'settings' => array(
        'file_directory' => 'YOUR_DIRECTORY_HERE',
)

For example:

 // Create the instance.
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => 'article',
      'label' => 'Article Image',
      'description' => 'An image for the article.',
      'required' => TRUE,
      'settings' => array(
        'file_extensions' => 'png jpg jpeg',
        'file_directory' => 'myModule/news', //your wanted attribute
        'max_filesize' => '20 MB',
        'alt_field' => 1,
        'title_field' => 1,
        'max_resolution' => '300x200',
        'min_resolution' => '200x125',
        'default_image' => 0,
      ),
  • 1
    Instead of adding duplicate answers to multiple questions, please tailor to the answer for the question or flag as a duplicate. Thanks. – mpdonadio Aug 8 '15 at 23:25

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