What have you tried?
For the time you can use Carbon, which is included in Laravel: http://carbon.nesbot.com/docs/
For the MB you just need to calculate the file size and show that: $bytes = File::size($filename);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
What have you tried?
For the time you can use Carbon, which is included in Laravel: http://carbon.nesbot.com/docs/
For the MB you just need to calculate the file size and show that: $bytes = File::size($filename);
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
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
Thanks, I will.
There is a few different methods here.
http://stackoverflow.com/questions/5501427/php-filesize-mb-kb-conversion
Rather than just pasting one here, I'll let you decide the best approach for your circumstances.
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;
}
}
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];
}
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','کتاب موفقانه آپلود شد.');
}
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) }}
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>
Check the namespace.
should be App\Helpers
If you've just made the file, try composer dump-autoload
Same namespace
'HumanReadable' => App\Helpers\HumanReadable::class,
Thank you so much @bashy
Thanks i got this working... I got exact same error i changed class name and filename as same
That works fine in my project. thanks @bashy
Please or to participate in this conversation.