First of all, I'm using drupal 6.
I'm creating one template and I need to write the path of the files from node attachments.
The Content Template module shows something like "$node->files[1290]->filepath" but the id (1290) is unique and not related to the node id.
I tried many drupal 5 and 4.7 solutions found in this site but none did work.

Comments

dman’s picture

The id will be either the file id, or the file revision id, depending on version or something...
But who cares?

You'll probably be wanting something like:

foreach((array)$node->files as $fileobj){
print( l($fileobj->filepath, $fileobj->filepath));
}

(untested)

jrglasgow’s picture

if you could post the section of your body variables that pertains to the files, I would help us help you. Please post it in <code> tags

patrickmedina’s picture

Dman, thanks for your answer, but your code didn't work :(

This is the output of the body variables in content template:

$node->nid
    634 
$node->type
    djsets 
$node->language
    pt 
$node->uid
    1 
$node->status
    1 
$node->created
    1221688456 
$node->changed
    1221688456 
$node->comment
    2 
$node->promote
    1 
$node->moderate
    0 
$node->sticky
    0 
$node->tnid
    0 
$node->translate
    0 
$node->vid
    634 
$node->revision_uid
    1 
$node->title **
    Teste DJ Sets 
$node->body
    <table id="attachments" class="sticky-enabled"> <thead><tr><th>Attachment</th><th>Size</th> </tr></thead> <tbody> <tr class="odd"><td><a href="http://www.mydomain.dev/sites/default/files/04_Marry_You.mp3">Marry You</a></td><td>5.72 MB</td> </tr> <tr class="even"><td><a href="http://www.mydomain.dev/sites/default/files/One_Thing.mp3">One Thing</a></td><td>5.08 MB</td> </tr> </tbody> </table> 
$node->log
$node->revision_timestamp
    1221688456 
$node->format
    1 
$node->name
    patrick 
$node->picture
$node->data
    a:1:{s:18:"admin_compact_mode";b:1;} 
$node->path
    djsets/teste-dj-sets-[field_data-yyyy]-[field_data-mm] 
$node->last_comment_timestamp
    1221688456 
$node->last_comment_name (NULL)
$node->comment_count
    0 
$node->taxonomy (array)

$node->files (array)

    $node->files[1289] (object)

        $node->files[1289]->fid
            1289 
        $node->files[1289]->uid
            1 
        $node->files[1289]->filename
            04_Marry_You.mp3 
        $node->files[1289]->filepath
            sites/default/files/04_Marry_You.mp3 
        $node->files[1289]->filemime
            audio/mpeg 
        $node->files[1289]->filesize
            5994143 
        $node->files[1289]->status
            1 
        $node->files[1289]->timestamp
            1221688410 
        $node->files[1289]->nid
            634 
        $node->files[1289]->vid
            634 
        $node->files[1289]->description
            Marry You 
        $node->files[1289]->list
            1 
        $node->files[1289]->weight
            0 

    $node->files[1290] (object)

        $node->files[1290]->fid
            1290 
        $node->files[1290]->uid
            1 
        $node->files[1290]->filename
            One_Thing.mp3 
        $node->files[1290]->filepath
            sites/default/files/One_Thing.mp3 
        $node->files[1290]->filemime
            audio/mpeg 
        $node->files[1290]->filesize
            5322605 
        $node->files[1290]->status
            1 
        $node->files[1290]->timestamp
            1221688422 
        $node->files[1290]->nid
            634 
        $node->files[1290]->vid
            634 
        $node->files[1290]->description
            One Thing 
        $node->files[1290]->list
            1 
        $node->files[1290]->weight
            0 

$node->build_mode (integer)
    0 
$node->readmore (boolean)
    FALSE 
$node->content (array)

    $node->content['body'] (array)

        $node->content['body']['#weight'] (integer)
            0 
        $node->content['body']['#value']
        $node->content['body']['#title'] (NULL)
        $node->content['body']['#description'] (NULL)
        $node->content['body']['#printed'] (boolean)
            TRUE 

    $node->content['files'] (array)

        $node->content['files']['#weight'] (integer)
            50 
        $node->content['files']['#value']
            <table id="attachments" class="sticky-enabled"> <thead><tr><th>Attachment</th><th>Size</th> </tr></thead> <tbody> <tr class="odd"><td><a href="http://www.mydomain.dev/sites/default/files/04_Marry_You.mp3">Marry You</a></td><td>5.72 MB</td> </tr> <tr class="even"><td><a href="http://www.mydomain.dev/sites/default/files/One_Thing.mp3">One Thing</a></td><td>5.08 MB</td> </tr> </tbody> </table> 
        $node->content['files']['#title'] (NULL)
        $node->content['files']['#description'] (NULL)
        $node->content['files']['#printed'] (boolean)
            TRUE 

    $node->content['#title'] (NULL)
    $node->content['#description'] (NULL)
    $node->content['#children']
        <table id="attachments" class="sticky-enabled"> <thead><tr><th>Attachment</th><th>Size</th> </tr></thead> <tbody> <tr class="odd"><td><a href="http://www.mydomain.dev/sites/default/files/04_Marry_You.mp3">Marry You</a></td><td>5.72 MB</td> </tr> <tr class="even"><td><a href="http://www.mydomain.dev/sites/default/files/One_Thing.mp3">One Thing</a></td><td>5.08 MB</td> </tr> </tbody> </table> 
    $node->content['#printed'] (boolean)
        TRUE 

$node->links (array)

    $node->links['comment_add'] (array)

        $node->links['comment_add']['title']
            Add new comment 
        $node->links['comment_add']['href']
            comment/reply/634 
        $node->links['comment_add']['attributes'] (array)

            $node->links['comment_add']['attributes']['title']
                Share your thoughts and opinions related to this posting. 

        $node->links['comment_add']['fragment']
            comment-form 
patrickmedina’s picture

Assigned: patrickmedina » Unassigned

jrglasgow?

jrglasgow’s picture

use code like this:

foreach ($node->files as $key => $value) {
   //  the key will be the file id, i.e. 1289 and $value will be the file object
   //  the file path is given in $node->file[$key]->filepath
   //  which is the same as $value->filepath
   //  once you have the filepath (relative to the Drupal install) you can either
   print '<a href="'. url($value->filepath) .'">Download the file ('. $value->description .')</a>';
   // or
   print l('Download the file:'. $value->description, $value->filepath)
}

the l function will return a link based on the path, the url function will just return a URL, not the whole link.

sampeckham’s picture

Great I was trying to work this out too, it's not obvious! However, can you tell me how I might adjust this for just one node with one attachment. Is there a quick way to just grab the $key value, without the for loop?

Thanks

s

jrglasgow’s picture

you could use array_keys()

// the array_keys() function returns an array of the keys, the first [0] item in 
// this array will be the key for the first file (at least it should be)
$keys = array_keys($node->files);
print l('Download the file:'. $node->files[$keys[0]]->description, $node->files[$keys[0]]->filepath)

this is pseudo code, it has not been tested

dman’s picture

Keep the foreach. It's more robust code, as I would not trust the index being zero, or array_pop returning right in the case of no files.
using a foreach will loop over zero, one, or more the same way.
If it bothers you, just break; out of the first successful loop.

sampeckham’s picture

Thanks for these responses, I have stuck with the for loop and that works fine in one case.

But I do have a question that relates to this, and have thrown it to the forums to avoid hijacking this thread: http://drupal.org/node/327085