Textpattern CMS support forum
- New Member
- Registered: 2012-05-21
- Posts: 6
PHP file extension
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)
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)
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.
<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" />
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:
README.textile installation.readme setup.md
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?