Convert File Size Into Kilo Mega Byte From Path in Laravel

How to get human-readable file size from public path and storage path in Laravel is today's tutorial. In this example, I will share with you, how to get file size in a human-readable format like kilo bye, megabyte, a terabyte from the path, or URL in Laravel. 

Getting file path or URL and converting it Into human-readable format in Laravel. After getting the file path, you will see the output such as 1.95 KB format. I will create a simple custom function to convert bytes into human-readable format.

So let's see the example code of get file path from storage and public and show human-readable byte format in Laravel.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class FileController extends Controller
    public function index()
        $fileSize = Storage::size('public/css/style.css');  //storage path
        $fileSize = File::size(public_path('css/style.css')); //public path
        return $this->convertUploadedFileToHumanReadable($fileSize);

    public function convertUploadedFileToHumanReadable($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)];
        return $size;


Read also: How to Get Human Readable File Size in PHP Laravel


Hope this getting file path and converting it into kilobyte or megabyte tutorial will help you.


Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js