Make a link to a file
Previous article Next articleUser Defined Tag to create a link to open a file, followed by the file size.
This article originated from the former CMSMS Wiki and the original author is Ralph Kutschera
The UDT forces the download of the file now! [HTML5!]
Create a new User Defined Tag, named "file_link" and copy-paste the following code in it:
// check if file exists
if(file_exists($url))
{
$linkname = isset($params['name']) ? $params['name'] : $url;
$filesize = filesize($url);
// beautify filesize
$suffix = "B";
if($filesize > 1024)
{
$filesize = $filesize / 1024;
$suffix = "KB";
if($filesize > 1024)
{
$filesize = $filesize / 1024;
$suffix = "MB";
if($filesize > 1024)
{
$filesize = $filesize / 1024;
$suffix = "GB";
}
}
}
$filesize = sprintf("%.2f", $filesize);
// edit this line, if you want a different representation of your link
echo '<a href="'.$url.'" download><b>'.$linkname.'</b> ('.$filesize.$suffix.')</a>';
}
else
{
// this is shown if the file you try to link can't be found
echo $linkname.' (Sorry, file not found)';
}
You can omit the tag attribute 'name' if you want the name of the file as the link text. The code also checks whether the file does exist and gives a '(Sorry, file not found)' otherwise.
How to use
Or in a module template you can do something like:
Working example
I use this UDT to download a .XML file in my blog FormBuilder template and stylesheet
Comment Form
ReviewManager
ReviewManager
0 Comments
No comments yet...