I'm using Drupal 6.x with the Filefield 6.x-3.10. I've used this for a content type theme file. Long story short, a user can upload a Microsoft Word file and have it be displayed on the page for download...

When I built the theme file for this content type, I decided to theme the file download links by looping through $node->foobar_files to get the file URLs so that I could generate a link list as follows:

   foreach($node->foobar_files as $k){
      var_dump($k['filepath']);<--- Doesn't have the word "node".
      print '<li><a href="'.$k['filepath'].'" title="'.$k['filename'].'">'.$k['filename'].'</a></li>';<--- DOES have the word "node".

In the above code, var_dump outputs string(66) "sites/default/files/foobar.docx"
In the printed link, however, the HREF shows "/node/sites/default/files/foobar.docx"

Is the word "node" being automatically added to the URL value from FileField or is this coming from somewhere else? The thing I can't understand here is that all attempts to change this value have been fruitless... I even tried to use a str_replace() on it and that didn't even work.

I don't see how the EXACT SAME VARIABLE can output 2 different results.

I'd appreciate some insight into this.


I think I've found where the problem is coming from but still not sure about the "fix": this seems to happen when I use "Clean URLs" (the Clean URLs seems to add the word "node" to the URL value).

So this begs the question, how can I theme FileField links to work correctly regardless of using Clean Urls?

If they break when I turn on Clean Urls, that tells me that the way I've themed them is incorrect. Again, I've looped through the $node->foobar_files array / object and I've output the links using something like $k['filepath'].

What other options do I have in all this? Do I need to put $k['filepath'] into some sort of validation / parsing function before printing it???

Category:bug» support
Priority:Critical» Normal
Status:Active» Fixed

The variable isn't changing in between the two lines where you're calling it. It's because it is a *relative* file path. That is, it doesn't contain the site root URL or a leading slash, it's just "sites/default/files/example.jpg". So if you're at node/1 and you make a link to "sites/default/files/example.jpg", your web browser will open the URL "node/1/sites/default/files/example.jpg". This is simply the way URLs work in anchor tags, it's not a bug.

In order to make your links work everywhere, you simply need to include the base URL. However since this base URL may change based how you've installed Drupal, the easiest and standard way of printing out a file URL is to wrap $file['filepath'] in file_create_url(). Additionally, your code would introduce XSS security holes if you used it as-is. You should wrap individual properties in check_plain() to ensure they can't execute JavaScript:

print '<li><a href="' . file_create_url($k['filepath']) . '" title="' . check_plain($k['filename']) . '">' . check_plain($k['filename']) . '</a></li>';

In the future, please don't file questions about custom code as a bug.

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.