PHP Format File Size Convert KB MB GB

Today, We want to share with you PHP Format File Size Convert KB MB GB.In this post we will show you Human Readable File Size with PHP, hear for convert size in bytes to KB, MB, GB in PHP we will give you demo and example for implement.In this post, we will learn about How to Convert File Size to Human Readable Format in PHP with an example.

PHP Format File Size Convert KB MB GB

There are the Following The simple About PHP Format File Size Convert KB MB GB Full Information With Example and source code.

Also Read :  Basic OOP PHP CRUD Operation Using MySQLi from Scratch

As I will cover this Post with live Working example to develop php check file size before upload, so the $_files size in php for this example is following below.

Simple You can Convert the size in bytes (PHP filesize MB/KB conversion) for a file into a more redable user-friendly format. so You can simple send the data in bytes to this PHP function:

<?php

function formatfileConvertsize($value) {

    //convert Bytes Logic
    if($value < 1024) {

        return $value . " bytes";

    }
    //convert Kilobytes Logic
    else if($value < 1024000) {

        return round(($value / 1024 ), 1) . "k";

    }
    //convert Megabytes Logic
    else {
        return round(($value / 1024000), 1) . "MB";
    }

}

?>

File Size Conversion with PHP

function formatfileConvertsize($bytes, $decimals = 2){
    $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
    $data = floor((strlen($bytes) - 1) / 3);
    return sprintf("%.{$decimals}f", $bytes / pow(1024, $data)) . @$size[$data];
}

Convert File Size to Human Readable Format in PHP

Also Read :  jQuery Ajax GET & POST REQUEST Methods PHP MySQLi

simple you can Call the PHP formatfileConvertsize() function and simple pass the filesize in bytes.

// Get file size
$size = filesize('pakainfo_project.zip');

// Convert file size
echo formatfileConvertsize($size);

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Format File Size Convert KB MB GB.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.