
有的同学附件很多 想的在前端显示所有附件的总大小, 由于正常标签没有,只能通过Php标签来实现 下面是具体代码方法:
代码
{eyou:volist name="$eyou.field.file_list" id="field"}
{eyou:if condition="$key eq 0"}
{php}$total_size = 0;{/php}
{/eyou:if}
{php}
$size_str = trim($field['file_size']);
$size_num = preg_replace('/[^0-9.]/', '', $size_str);
$size_unit = preg_replace('/[0-9.]/', '', $size_str);
// 单位转换为字节
switch(strtoupper(trim($size_unit))) {
case 'KB': $size_bytes = $size_num * 1024; break;
case 'MB': $size_bytes = $size_num * 1024 * 1024; break;
case 'GB': $size_bytes = $size_num * 1024 * 1024 * 1024; break;
default: $size_bytes = $size_num; // 默认为字节
}
$total_size += $size_bytes;
{/php}
{/eyou:volist}
<div class="text-center margin-top">
<strong>总文件大小:</strong>
{php}
// 字节转换为易读格式
function formatSize($bytes) {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$i = 0;
while ($bytes > 1024 && $i < 4) {
$bytes /= 1024;
$i++;
}
return number_format($bytes, 2) . ' ' . $units[$i];
}
echo formatSize($total_size);
{/php}
</div>
效果:

示例二:
{eyou:volist name="$eyou.field.file_list" id="field"}
{eyou:if condition="$key eq 0"}
{php}$total_size = 0;{/php}
{/eyou:if}
{php}
$size_str = trim($field['file_size']);
$size_num = preg_replace('/[^0-9.]/', '', $size_str);
$size_unit = preg_replace('/[0-9.]/', '', $size_str);
// 单位转换为字节
switch(strtoupper(trim($size_unit))) {
case 'KB': $size_bytes = $size_num * 1024; break;
case 'MB': $size_bytes = $size_num * 1024 * 1024; break;
case 'GB': $size_bytes = $size_num * 1024 * 1024 * 1024; break;
default: $size_bytes = $size_num; // 默认为字节
}
$total_size += $size_bytes;
{/php}
<a href="{$field.downurl}" {eyou:if condition="$field.extract_code !=''"}data-clipboard-text="{$field.extract_code}"{/eyou:if} class="btn button button-big button-block text-center bg-yellow border-none radius-big margin-top">
<i class="iconfont icon-xiazai margin-right"></i>{$field.server_name}
</a>
{eyou:if condition="$field.extract_code !=''"}
<div class="text-center text-gray margin-small-top">
提取码:{$field.extract_code}
</div>
{/eyou:if}
{/eyou:volist}
<!-- 循环外展示总大小 -->
<div class="text-center margin-top">
<strong>总文件大小:</strong>
{php}
// 字节转换为易读格式
function formatSize($bytes) {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$i = 0;
while ($bytes > 1024 && $i < 4) {
$bytes /= 1024;
$i++;
}
return number_format($bytes, 2) . ' ' . $units[$i];
}
echo formatSize($total_size);
{/php}
</div>
