
有的同學附件很多 想的在前端(duan)顯示所有附件的總大(da)小, 由(you)于正(zheng)常標簽沒(mei)有,只能通(tong)過Php標簽來實現 下面是(shi)具(ju)體代碼方法:
代碼(ma)
{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; // 默認為字(zi)節
}
$total_size += $size_bytes;
{/php}
{/eyou:volist}
<div class="text-center margin-top">
<strong>總文件(jian)大(da)小(xiao):</strong>
{php}
// 字(zi)節轉換為(wei)易讀格(ge)式
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);
// 單位轉(zhuan)換為字節
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; // 默認(ren)為(wei)字節
}
$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}
<!-- 循(xun)環外(wai)展示總大小 -->
<div class="text-center margin-top">
<strong>總文件大(da)小:</strong>
{php}
// 字節轉換為(wei)易讀格式
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>
