How to get file size?

Help me to understand.
I am a very “small” php coder :slight_smile:
In the template, I get the file name and link to the file

<?= $item->locname()->text() ?>
<?= $item->locfile()->toFile() ?>

How do I get the file size?

  F::niceSize(mixed $size): string

That just formats the size in a string to something human readable.

To get the actual size of file you need this one instead…

<?= F::size($item->locfile()->toFile()) ?>

If you stored that in a variable instead of echoing it, you could run it through niceSize to get into something more readable instead of a string of numbers.

<?php $size = F::size($item->locfile()->toFile());?>
<?= F::niceSize($size) ?>
1 Like

Using blueprints in the panel, I upload a pdf file.
I add it to a field and give it a name in another field.
I get locfile and locname via
‘items’ => $page->locfilelist1()->toStructure()
but variant F::size($item->locfile()->toFile()) returns 0, not null

<?php foreach ($items as $item): ?>
<td><?= F::size($item->locfile()->toFile()) ?></td>
<td><a href="<?= $item->locfile()->toFile() ?>">Download</a></td>
<td><?= $item->locname()->text() ?></td>
<?php endforeach ?>

Can you post the blueprint?

on a side note this:

<a href="<?= $item->locfile()->toFile() ?>">Download</a>

Should be:

<a href="<?= $item->locfile()->toFile()->url() ?>">Download</a>
1 Like
        label: 1. Локальные акты регламентирующие управление учреждением
        type: structure
            label: Наименование
            type: text
            label: Файл
            type: files
            multiple: false

@texnixe Sonja please help me figure out the code :pray:

$items = $page->locfilelist1()->toStructure();
foreach ( $items as $item ) : 
  if ( $file = $item->locfile()->toFile() ) :
   // now you have a file object
    echo $file->url(); 
    echo $file->niceSize();

Thank you so much my friend! :+1:
Everything worked out!

On a side note, @texnixe ->niceSize() is not in the docs. I looked yesterday (at least it does not show up when you search but F::niceSize does)

1 Like

I know, we are aware of this, it’s a problem with how the documentation is generated and this inheritance not reflected.

Friends, tell me.
Is it possible to insert magic letters or numbers into these niceSize () brackets, so that it would turn out as in the screenshot.
Thank you very much.

Unfortunately, the number of decimals for rounding is hardcoded in the niceSize() method.

Maybe it would make sense in your case to use a custom file method instead of the built-in method.

Edit: Sent you a PM.

1 Like

Hi @texnixe and @obrsk did you ever cook up a solution for a nicerSize() method that you are willing to share? I’m after the same thing: rounded filesizes without decimals…

This is the overwritten method, the only change to the original niceSize() method is the 2nd parameter passed to the round() function. You can even make this a parameter to make it more versatile.

Do you also need to overwrite the units like Mark?

 'fileMethods' => [
        'customSize' => function() {
            // file mode
            $size = (int) static::size($this->path());

            // avoid errors for invalid sizes
            if ($size <= 0) {
                return '0 KB';

            // the math magic
            return round($size / pow(1024, ($i = floor(log($size, 1024)))), 1) . ' ' . F::$units[$i];

I just returned here to post my solution only to find out about your reply (strangely enough I had not received any notification…)

Although the rounding part is pretty much identical, my approach consisted of a rather cobbled together function that I used on the raw byte output instead of this elegant file method. Just like you suggested, I had actually added the rounding precision as a parameter (0 as default).

Your support is nothing short of amazing texnixe, many thanks for this!

1 Like