PHP code example of nabeghe / file-size

1. Go to this page and download the library: Download nabeghe/file-size library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nabeghe / file-size example snippets


use Nabeghe\FileSize;

FileSize::getAllUnits();     // ['B', 'KB', 'MB', 'GB', ...]
FileSize::getAllUnits(true); // ['b', 'Kb', 'Mb', 'Gb', ...]

FileSize::isValidUnit('MB');  // true
FileSize::isValidUnit('xyz'); // false

FileSize::format(13);         // 13
FileSize::format(14);         // 14
FileSize::format(100.456);    // 100.46
FileSize::format(100.000001); // 100
FileSize::format(313.1314, 3) // 313.131

FileSize::convert(1, 'GB', 'MB');               // 1024
FileSize::convert(1024, 'KB', 'MB');            // 1
FileSize::convert(1, 'GB', 'Mb');               // 8192
FileSize::convert(1234, 'MB', 'GB', 1);         // 1.2
FileSize::convert(1, 'Gb', 'Mb');               // 1024
FileSize::convert(1, 'Gb', 'Mb', false, false); // 1000 (decimal)

FileSize::compare(1, 'GB', 1024, 'MB'); // 0 (equal)
FileSize::compare(1, 'GB', 2, 'GB');    // -1 (smaller)
FileSize::compare(3, 'Mb', 2, 'MB');    // -1 (smaller, because 3Mb ≈ 0.375MB)

$unit = FileSize::detectUnit(123456789, false, $finalSize); // binary=1024
// $unit => 'MB', $finalSize => 117.74

$unit = FileSize::detectUnit(123456789, false, $finalSize, false); // decimal=1000
// $unit => 'MB', $finalSize => 123.46

FileSize::readable(123456789);                                  // "117.74 MB"
FileSize::readable(123456789, false, null, false);              // "123.46 MB"
FileSize::readable(123456789, true);                            // "117.74 Mb"
FileSize::readable(123456789, true, null, false);               // "123.46 Mb"
FileSize::readable(1024**3, false, ['GB' => 'Gigabyte'])        // "1 Gigabyte"
FileSize::readable(1024**3, false, ['GB' => 'Gigabyte'], false) // "1.07 Gigabyte"

FileSize::readableFromUnit(2048, 'KB');              // "2 MB"
FileSize::readableFromUnit(2000, 'KB', null, false); // "2 MB"
FileSize::readableFromUnit(2048, 'Kb');              // "2 Mb"
FileSize::readableFromUnit(1, 'Gb');                 // "1 Gb"

FileSize::parse('1.5 GB');   // [1.5, 'GB']
FileSize::parse('  200 Kb'); // [200, 'Kb']
FileSize::parse('3mb');      // [3.0, 'mb']

FileSize::percentage(512, 'MB', 1, 'GB'); // 50.0
FileSize::percentage(100, 'Kb', 1, 'Mb'); // 10.0