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.

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:


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

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);

How to check the size of a file in PHP?

You can use the filesize function in PHP to get the size of a file in bytes. Here’s an example of how you can use this function:

$filename = "example.txt";

if (file_exists($filename)) {
    $filesize = filesize($filename);
    echo "The size of the file is: " . $filesize . " bytes.";
} else {
    echo "The file does not exist.";
}

In this example, the file_exists function is used to check if the file example.txt exists, and if it does, the filesize function is used to get the size of the file in bytes. The size of the file is then echoed to the screen in a message that says “The size of the file is: [size] bytes.”

Note that the filesize function can also be used to get the size of other types of files, not just text files, and that it returns the size of the file in bytes, which you can then format in a more human-readable format if desired.

Format bytes to kilobytes, megabytes, gigabytes

You can format the size of a file in bytes to kilobytes, megabytes, or gigabytes by dividing the size in bytes by the appropriate value. Here’s an example of how you can do this in PHP:

$filename = "example.txt";

if (file_exists($filename)) {
    $filesize = filesize($filename);
    $size = $filesize;

    $units = array('B', 'KB', 'MB', 'GB', 'TB');
    for ($i = 0; $size >= 1024 && $i < count($units) - 1; $i++) {
        $size /= 1024;
    }

    $formatted_size = round($size, 2) . " " . $units[$i];
    echo "The size of the file is: " . $formatted_size;
} else {
    echo "The file does not exist.";
}

In this example, the file_exists function is used to check if the file example.txt exists, and if it does, the filesize function is used to get the size of the file in bytes. The size of the file is then divided by 1024 and rounded to 2 decimal places to get the size in kilobytes. This process is repeated until the size is either less than 1024 or until the end of the $units array is reached. The resulting size is then formatted with the appropriate unit and echoed to the screen in a message that says "The size of the file is: [formatted 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.

Leave a Comment