Skip to content

Instantly share code, notes, and snippets.

@delphinpro
Created November 5, 2018 11:38
Show Gist options
  • Select an option

  • Save delphinpro/d97e4786cca877a48355d66992b66b5f to your computer and use it in GitHub Desktop.

Select an option

Save delphinpro/d97e4786cca877a48355d66992b66b5f to your computer and use it in GitHub Desktop.
Сниппет MODX — Ссылка на скачивание файла
<?php
$file = isset($file) ? $file : null;
$title = isset($title) ? $title : 'Скачать';
if (!$file) return '';
$filepath = MODX_BASE_PATH.$file;
if (!is_file($filepath) || !is_readable($filepath)) return 'Ошибка чтения файла<br>('.$file.')';
$fi = pathinfo($file);
$ext = $fi['extension'];
if (!function_exists('human_filesize')){
/** @author http://php.net/manual/ru/function.filesize.php#120250 */
function human_filesize($bytes, $decimals = 2) {
$factor = floor((strlen($bytes) - 1) / 3);
$r = '';
$sz = ['к','М','Г','Т'];
if ($factor > 0) $r = $sz[$factor - 1];
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . $r . 'б.';
}
}
$size = filesize($filepath);
if ($size) {
$size = human_filesize($size);
}
$out = '
<div class="download-doc mt-0.5">
<a class="download-doc__button btn btn_icon btn_block" href="'.$file.'">
[[svgIcon? &icon=`download` &cls=`btn__icon`]]
<span class="btn__content">'.$title.'</span>
</a>
<div class="download-doc__meta">.'.$ext.''.($size ? ' ('.$size.')' : '').'</div>
</div>
';
return $out;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment