Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By jonathanchris on
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
it's an array
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/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thanks!
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!
almost.
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/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
umm...
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?
array_shift()
array_shift() it off, or foreach loop through them
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
array_shift?!?
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
TMTOWTDI
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...
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thanks
I ended up using the foreach loop to find the file that is an image, if there was one. Thanks for your help!