I'm trying to access the filename and file path for a node. I have the upload module enabled and have uploaded files with my nodes.

I've tried $node->files->filepath, but it doesn't seem to be doing the trick.

If anyone knows the missing link here, I'd appreciate it.

Comments

dman’s picture

$node->files->filepath

Did you mean $node->files[n]->filepath?

try print_r($node->files); to see what you're doing wrong.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

jonathanchris’s picture

I took a peek at what the node object was holding and figured out a way to grab it with:

$node->files[$node->nid]->filepath

Thanks for the help!

dman’s picture

Beware. It may be a coincidence that the file ID matches the node ID for you. There may be more than one file per node, they run their own index.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

jonathanchris’s picture

Then how would you select the first file without the id#, I see what you're saying and for the most part I think there will only be one file per node, but can't continue to guarentee that. So how would you only select, say the first file for a specific node?

dman’s picture

array_shift() it off, or foreach loop through them

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

jonathanchris’s picture

Won't that take it off the array. Therefore after the first time it will be gone. If I'm wrong please let me know. That would make it easy if it won't destroy it. Actually, now that I think about it, it should be ok, because it'll just shift a copy of it off... i think.

Please clarify for me, before I try. I don't want to start losing content. Thanks

dman’s picture

If you are planning to be editing it, changing it and SAVING it again just through viewing it in your theme, then yeah, array_shift would not be what you want. For non-persistant inspection of the data however, it doesn't really matter.

This may look ungainly, but may work for you...

foreach($node->files as $file){  break; }
print_r($file);

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

jonathanchris’s picture

I ended up using the foreach loop to find the file that is an image, if there was one. Thanks for your help!