Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Khudadad's avatar

Human readable file size and time

Hi, I want to display the size of a file in MB in the views and also the timestamps in human readable format, but I don't know how to do this any help. Thanks

0 likes
16 replies
Khudadad's avatar

Thanks @bobbybouwmann for the reply. I tried to show the a list of books with their titles, authors ,size in MB and the date uploaded. (i.e uploaded 2 days ago and so on). About the size of file I just see numbers

bobbybouwmann's avatar

I don't know what kind of value you have for the size of the books, just Google how you can get from bytes to MB

swimlappy's avatar

I use a simple function like this to format file sizes

    /**
     * Format bytes to kb, mb, gb, tb
     *
     * @param  integer $size
     * @param  integer $precision
     * @return integer
     */
    public static function formatBytes($size, $precision = 2)
    {
        if ($size > 0) {
            $size = (int) $size;
            $base = log($size) / log(1024);
            $suffixes = array(' bytes', ' KB', ' MB', ' GB', ' TB');

            return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
        } else {
            return $size;
        }
    }
6 likes
bashy's avatar

Something like this would do

public static function bytesToHuman($bytes)
{
    $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];

    for ($i = 0; $bytes > 1024; $i++) {
        $bytes /= 1024;
    }

    return round($bytes, 2) . ' ' . $units[$i];
}
7 likes
Khudadad's avatar

Thanks all, but I don't know where to use it? this is my controller:

 public function store(BookRequest $request)
{
        $book = new Book();
        $book->title = $request->input('title');
        $book->isbn = $request->input('isbn');
        $book->author = $request->input('author'); 
        $book->description = $request->input('description');
    if($request->hasFile('file'))
    {
        $file = $request->input('file');
        $fileSize = 
        $fileName = date('Y') . "_" . $file->getClientOriginalName();
        $distination_path = 'books/';
        $file->move($distination_path, $fileName);
        $book->book_path = $distination_path . $fileName;
    }
        $book->user_id = \Auth::user()->id;
    $book->save();
    return redirect('/library')->with('message','کتاب موفقانه آپلود شد.');
}
bashy's avatar

I normally create a folder in app called Helpers. I then create a file in that called my company's name.

In that file you can define a simple class like so

<?php

namespace App\Helpers;

class SomeClass
{
    public static function bytesToHuman($bytes)
    {
        $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];

        for ($i = 0; $bytes > 1024; $i++) {
            $bytes /= 1024;
        }

        return round($bytes, 2) . ' ' . $units[$i];
    }
}

You can then alias it in config/app.php 'SomeClass' => App\Helpers\SomeClass::class,. Then you can simple call it in views or w/e.

{{ SomeClass::bytesToHuman(72134) }}
6 likes
Khudadad's avatar

I'm getting error:

ErrorException in AliasLoader.php line 66: 
Class 'App\Helpers\HumanReadable' not found

The alias:

'HumanReadable' => App\Helpers\HumanReadable::class,

Veiw:

<td>{{ HumanReadable::bytesToHuman($book->size) }}</td>
fraserk's avatar

Check the namespace.

should be App\Helpers

bashy's avatar
bashy
Best Answer
Level 65

If you've just made the file, try composer dump-autoload

2 likes
Khudadad's avatar

Same namespace

'HumanReadable' => App\Helpers\HumanReadable::class,
afsal96's avatar

Thanks i got this working... I got exact same error i changed class name and filename as same

1 like

Please or to participate in this conversation.