-1

I can't find the info through any search string I can think of. I need to retrieve the complete path including the filename for a file stored by a node.

public://module_name/filename.ext I can get the public://module_name part but not the file name. I think I am having a basic misunderstanding on how Drupal stores files. I have written a custom module and it puts the file where I specify: public://my_module. If the file is an Image (jpg) there is no problem. I can display the file name with the generic file format. so I know that the information is available, I just cant figure out how to access it.

can any one point me at an example, API description or tutorial that covers how to access a file in the current node?

0

This is not the 100% correct way of doing it because I'm accessing the property manually and I'm also generating the image html which can be automatically generated but maybe it can help as a simple example. In your theme's node.tpl.php write:

<?php
if (isset($node->your_field[LANGUAGE_NONE][0])) {
    $o_img = $node->your_field[LANGUAGE_NONE][0];
    $s_uri = $o_img['uri'];
    $s_url = file_create_url($s_uri);
    $s_html = "URI: {$s_uri} | URL: {$s_url}";
    print $s_html;
}
?>

Regards

EDIT: How to better read a field (I haven't tried with image fields). Notice you don't have to specify the language because everything is automatically handled by Drupal based on your language preference and fallback configuration.

$aa_fields = field_get_items('node', $node, 'my_field');
foreach ($$aa_fields as $a_field) {
    $a_rendered_field = field_view_value('node', $node, 'my_field', $a_field);
    $s_html = $a_rendered_field['#markup'];
}

In order to avoid creating clutter in your node template file, it's better to move this code to your theme's template.php or, if you plan to use your code with other themes, to your own custom module.

| improve this answer | |
  • as far as I can tell this generates entity missing bundle errors. I suppose I just don't comprehend it..i need to get a string back that goes something like "public://bigimage/file.zif " so I can pass it toi a custom file viewer. Still Thanks for your reply. – Bill Oct 11 '17 at 5:47
  • Where are you using the code? If you wanted to use the above code in your node template file (node.tpl.php) you should substitute $o_node by $node and 'my_field' by the actual name of the field for your image. The error sounds to me like you don't have any image uploaded to that particular node or that the field you are indicating doesn't exist at all. Also, keep in mind that if you put the above code in node.tpl.php, it will apply to all type of contents and your image field probably won't be found in some of them. So you would need to check if the field exists and contains any image. – NoRandom Oct 11 '17 at 7:37
  • My situation is that I have rewritten an existing module. I have to defined fields the I cal bigimage_image and bigimage_zif The ZIF file is a modified big tiff that I need to send to java script viewer. I cant for the life of me get the filename to append to the path. I'm not working with the theme template files. I'm trying to generate all the markkup in a function called function theme_bigimage($variables) which lives in my .module file. This function works for a different file structure I juist nee to convert it so that It can find a single file. – Bill Oct 12 '17 at 4:29
0

I looked at the functional code today and I find that the problen lies in the definition of $vaariables. In the functional code I can retrieve the path to the jpeg image as $variable['filepath'] in the non functional code the path does not include the file name. So $varable is not being befined propperly in my new module. I will have to study up on it. I do have some references to the subject. Thanks for the help and advise.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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