Jump to content

Find filenames of uploaded files?


wilsea
 Share

Recommended Posts

Having just started with ProcessWire and delighted to find it easier than I feared it might be. 

My present stumbling block is trying to find the name(s) of files uploaded to a page, so that the name(s) can be made into llinks so they can be downloaded.

I've tried doing a var_dump of $page, $files, and $page->file but no sign of a reference to my uploaded file.  I know I could use php to query the appropriate directory, but i suspect ProceesWire has a more elegant way of doing it that I'm missing so far.

Can anyone show me the light?

Thanks for reading.

  • Like 1
Link to comment
Share on other sites

Welcome to ProcessWire!

Assuming your file field is called "files" in the admin, you could do this in your template:

<?php
foreach ($page->files as $file) {
   echo "<a href='$file->url'>$file->name</a>
}
You could also substitute the name with the contents of the description field too.

A pretty complete list of functionality can be found on the cheatsheet at http://cheatsheet.processwire.com

  • Like 1
Link to comment
Share on other sites

Thanks Pete, your suggestion worked like a charm! Yes I've been turning to the cheatsheet a lot, but at this stage sometimes a cheat sheet is not enough. I couldn't find the syntax you suggested on the cheat sheet. Obvious once it's pointed out. Thanks so much for the quick reply!

Link to comment
Share on other sites

Now for some gravy to go with the solution provided earlier.

When I add a file field, I automatically get a chance to add a description, which of course can be displayed any way I like. I'd like to be able to have another field as well as the description field for a "friendly" file name . So if one page is associated with three files, each of those files would have a description and a friendly name. Is there an easy way to do that? 

I tried putting a file field into a repeater field, but that doesn't seem to work very well as the file field seems to be a repeater field by default. Am I on the right track in thinking it might require a hook to tweak the file field? ? ? ?

Link to comment
Share on other sites

You could absolutely do a repeater, with the image field restricted to one image (so that each image corresponds to one text field in the repeater). But this problem has been tackled very well by Ryan, here:

http://processwire.com/talk/topic/417-extending-image-field/

Also, I've been given this suggestion some time ago, and it has been very useful: the forum search engine isn't the greatest, but if you search in google, with something like "my search site:processwire.com" it can be a whole lot better :)

  • Like 1
Link to comment
Share on other sites

Thanks Pete, your suggestion worked like a charm! Yes I've been turning to the cheatsheet a lot, but at this stage sometimes a cheat sheet is not enough. I couldn't find the syntax you suggested on the cheat sheet. Obvious once it's pointed out. Thanks so much for the quick reply!

No problem. Not sure what you mean by "the syntax" though? The foreach loop is normal PHP code (more control structures here: http://www.php.net/manual/en/language.control-structures.php ), so you're unlikely to find that on the site docs purely because you could be doing anything with a set of pages, not necessarily looping through them to display them so the cheatsheet doesn't make assumptions. There are quite a few examples of this type of code in the default template (head.inc has a few loops and examples of urls and titles etc being echo'd to the page).

On to your additional fields for your file field - you were right with repeaters, but you will want to edit your file field and set the max number of files to be 1 (it's set to zero by default which allows unlimited files to be uploaded). If you use your files field on other templates where you do want to allow multiple uploads in that field, you might want to clone the field and call it something else so you can have one file field with just 1 upload allowed for your repeater and another field for other uses that allows unlimited files.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...