Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-05-21 11:05:30

New Member
Registered: 2012-05-21
Posts: 6

PHP file extension

Hi all,

Im working on a files list, ideally I will have them display like so – My File Name (pdf)
The file name will be hyperlinked and the extension will follow (I may extend this to display an icon).

Since there isnt a specific tag for the extension im looking at using the php’s built in function pathinfo like so:

echo $file = ‘<txp:file_download_name />’;
$ext = $file[‘extension’];

This gives me error: File tags cannot be used outside a file context. And Im not too sure if I can put a textpattern tag inside a php variable like this.

Any advice? Im trying to steer clear of plugins if possible.

Last edited by a7atylor (2012-05-21 11:07:01)


#2 2012-05-21 11:38:41

Core designer
From: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 2,806

Re: PHP file extension

Hmmm, interesting one. How about submitting a suggestion for that as part of the core tags in the Development forum? Could be quite useful.

It doesn’t need to work out file types as such, just simply display the file extension, so I think this could be quite easy to implement. Maybe as additional attributes for the <txp:file_download_name /> tag.

Last edited by philwareham (2012-05-21 11:40:44)


#3 2012-05-21 12:38:44

From: Leeds, UK
Registered: 2006-01-29
Posts: 7,750

Re: PHP file extension


If you are embedding Txp tags like that you need to use parse() around them. Alternatively, all tags have a functional equivalent with the attributes as an array of args:

$file = file_download_name(array());


$file = file_download_name(array('title' => '1'));

Either way, as you found, you need to use it inside a file download container tag or you get a warning.


A <txp:file_download_type /> would be a useful addition. Adding an attribute to <txp:file_download_name> could get tricky, e.g. what would you expect to get out if you did this:

<txp:file_download_name title="1" extension="1" />

? Confuse-a-thon.

Of course, a type is not as simple as it seems. We could rely on the file extension — a Windows convention (harking back to DOS restrictions, maybe?) — but files like:


are all arguably some form of Text (.txt) file and should probably display as such. This leads to the rather sticky road of MIME types which makes my brain itch. Can we rely on dot-extension? Would <txp:file_download_extension /> be better, returning nothing for files that are extensionless?

The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp


Board footer

Powered by FluxBB