Search

I want to filter a datasource by the filename from a file upload field, yet I can’t seem to do it.

I would’ve expected to be able to choose the field in my datasource editor, then add the param to filter it with. It always returns no results in the xml…

I can see that the filename is under a sub node called ‘filename’. Why can’t my datasource find it?

A slash is required before your param value.

Thanks for the answer, but that doesn’t make sense to me. Where would I put the slash?

Can you give me an example?

Is the param set? Do you see it in your parameter pool (in debug mode)? If yes, {$paramname} should work.

Basically, I want to put the following in the url

rf645-film4-0008

and then translate that into a filename, to match my file, xml:

            <entry id="33">
            <image size="54 KB" path="/projects-images" type="image/jpg">
                <filename>RF645_film4_0008.jpg</filename>
                <meta creation="2010-04-05T20:45:01+01:00" width="640" height="480" />
            </image>
            <uploaded time="20:44" weekday="1">2010-04-05</uploaded>
            <project>
                <item id="3" handle="a-test-project" section-handle="projects" section-name="Projects">A Test Project</item>
            </project>
        </entry>

So I wanted to filter the image field (which is a file upload field) using something like:

translate(concat({$image},'.jpg'),'-','_')

where $image matches the url string ‘rf645-film4-0008’

Is this even possible??

It would be much easier (and safer) to put the real filename in the URL. In this case this is simple logic (i.e. “entry list” pages and “full entry” pages, the latter filtering a Data Source with a parameter = portion of the URL). That would not be too different from what you find in the default workspace of Symphony — there it’s articles, for you it’s files.

Once you got it working, you can think about changing your URLs… But try and think simple first. (This is “Symphony-Fu”.)

I’ve tried that and it aint workin… Is the file upload field filterable? It doesn’t show up in the Parameter Output section of the Datasource Editor…

Parameter output is used to filter another datasource! Yes, it is filterable. You can select it in the “Filter Results” part of the datasource edit page. But, to be honest, I don’t know if it can be filtered by ID or handle (or both).

May someone knows?

I’ve tried that and it aint workin… Is the file upload field filterable? It doesn’t show up in the Parameter Output section of the Datasource Editor…

They are two independent things — a File Upload field is filterable (you can filter entries by the filename or file type) but by default is not capable of being used as an Output Parameter.

I understand that, just thought that if one can’t use file upload field for parameter output, then how can it be filterable?

There isn’t a handle for uploaded files either, don’t know if this is a bug or not… I would have thought it would filter by the node in the fields output xml, but it isn’t.

Any one have a clue?

Ok, posted that at the same time as Nick, mine came out second :(

How can I filter by filename Nick?

Thanks in advance!

I understand that, just thought that if one can’t use file upload field for parameter output, then how can it be filterable?

Because you might want to filter entries based on a particular file type, perhaps. Imagine a scenario where you want just entries that have JPEG images, or .mov files.

There isn’t a handle for uploaded files either, don’t know if this is a bug or not

There are no handles for File Upload fields. However you can filter based on filename, either match the entire filename or with the regexp: filter to match partial filenames.

How can I filter by filename Nick?

Pop the path and filename into a filter on a File Upload field.

{$url-filename}

For example. Where your page is:

/?filename=/uploads/myimage.jpg

I haven’t tried this right now, I’m just going by the code I’m looking at in field.upload.php.

Urgh this is so confusing.

I’ve tried putting the filename into the url so my url is:

/projects/a-test-project/filename.jpg/

It’s the projects section, a test project (which filters the projects and images datasource, and then filename.jpg image (which I want to filter the images datasource based on filename). I’ve then added the filter to my datasource as

{$image}

which corresponds to my pages url strings

project/image

It’s not filtering the image though, I always get a no results in the xml

Urgh…

Do you see the param in your param pool? (If not, have you set up your page to make this part of the URL a parameter?)

Yes and yes…

I think this may be a bug, as I’m doing everything as I should be…

I guess I really just need to know exactly what should I be putting in the datasource filter box using

{$image}

as the param name (which is being output correctly), to filter a file uploaded with the file upload field.

As I alluded to above, I believe a standard filter on a File Upload filters the “file” column in the database which actually contains the path to the image relative to the workspace. Therefore if you aren’t including the full path in your filter then you’ll need to use a regex prefix on your filter.

Perhaps something like:

regexp:{$filename}$

With a value of myimage.jpg it should evaluate as:

regexp:myimage.jpg$

That regex should find matches where the value ends with ($) “myimage.jpg”.

Sounds like File Upliad filtering might need some more attention to make it more useful in 2.1.

Sorry for not being more explicit before.

I think all you need in your datasource is actually this:

/{$image}

instead of a simple yet wrong:

{$image}

Given that, as Nick said, you’re uploading images in the root of your workspace.

Thanks for the clarification guys, got it working with Nick’s answer. I’m so glad I could get it to work, it’s been bugging me for days how to do that, so relieved!

I don’t seem to be able to find any documentation on what can be done in the datasource filter box, other than in the concepts section here. I really need to be able to do the following xpath on the parameter

translate(concat({$image},'.jpg'),'-','_')

so that I can have a friendlier url, so that

/projects/a-test-project/Photo_5.jpg/

could be

/projects/a-test-project/photo-5/

Does anyone know what expression I would need to do that, incorporating Nick’s solution for filtering the field?

I know I ask a lot sometimes ;)

Does anyone know what expression I would need to do that, incorporating Nick’s solution for filtering the field?

Unfortunately this isn’t possible since File Upload fields don’t have handles associated with them. You can filter on the path/filename, or a regular expression thereof, but not a handle-ised version of the filename.

You have an alternative. You could use the Unique File Upload field which appends a timestamp onto the end of the filename:

Photo_5-1234567890.jpg

Provided you don’t upload two files at the same second (unlikely) then this will be a unique string for your image. You could then just pass this string in your URL:

/projects/a-test-project/1234567890/

And then filter the Unique Upload Field with similar principles:

regexp:{$filename}.jpg$

This will find any image with the filename ending 1234567890.jpg.

Is that close?

Alternatively you could try adding a Reflection Field (which can be hidden from public view) to the section which stores this handle version upon each save of your entry. The Reflection Field allows you to specify XPath that acts upon the built <entry> node when you save an entry. So if you added a Reflection Field named “Image Handle” and give it the expression:

translate(substring-before(entry/image/filename,'.jpg'),'_','-')

This should strip off the .jpg and save a rough handle-ised version of the file name as this field’s value.

You should then be able to filter on this value in a Data Source, as you would a normal text field.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details