Giter VIP home page Giter VIP logo

php-filesize's Introduction

Latest Stable Version Total Downloads

php-filesize

A flexible package for handling file sizes and converting between units.

Installation

Include in your project, or, install with Composer:

$ composer require chrisullyott/php-filesize

Instantiate

A FileSize object, both on creation and within its methods, understands just about any expression of data size. You may instantiate it with a size, or leave it initially empty.

use ChrisUllyott\FileSize;

$size = new FileSize('500 GB');

Convert between units

Use as() to export the size in another format.

echo $size->as('MB'); // 512000

A variety of file size strings are supported here as well.

echo $size->as('megabytes'); // 512000

The second argument specifies decimal precision (default is 2).

echo $size->as('TB', 3); // 0.488

User-friendly formatting

Use asAuto() to get a user-friendly string:

$size = new FileSize('1234522678.12 KB');

echo $size->asAuto(); // '1.15 TB'

Optionally, asAuto() also provides a decimal precision.

$size = new FileSize('1234522678.12 KB');

echo $size->asAuto(5); // '1.14974 TB'

Or, simply echo the object for the same functionality:

echo $size; // '1.15 TB'

Modify the size

To make changes, use add(), subtract(), multiplyBy(), and divideBy().

$size = new FileSize('4 GB');

$size->add('2G')
     ->subtract('1 gigabytes')
     ->multiplyBy(4)
     ->divideBy(2);

echo $size; // '10.00 GB'

Negative values are supported. In the case below, 1.2 megabytes are subtracted:

$size->add('-1.2mb');

You may also use add() and subtract() with an array of values:

$size->add(['50mb', '140mb', '1.2mb']);

Number base

The second argument of the constructor is the number base, which accepts either 2 (binary) or 10 (decimal). We use binary by default. To handle sizes in decimal:

$size = new FileSize(10921134, 10);

echo $size; // '10.92 MB'

Decimal separator

The third argument of the constructor is the decimal separator, which is a period . by default. Here, you can use a comma instead. The chosen decimal separator will be used both to parse numbers properly, and also to format them on output.

$size = new FileSize('1.234.522.678,12 KB', 2, ',');

echo $size; // '1,15 TB'

php-filesize's People

Contributors

chrisullyott avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

php-filesize's Issues

Unexpected results given very large sizes

Since file sizes are stored in bytes, the utility is limited to dealing in byte counts up to PHP_INT_MAX. So when the maximum integer is 9223372036854775807, this means the largest possible size is 8 exabytes. An example test:

$size = new FileSize('8.1 EB');
$this->assertSame($size->asAuto(), '8.1 EB');
--- Expected
+++ Actual
@@ @@
-'-7.9 EB'
+'8.1 EB'

A third argument for number delimiter

This is a tiny beautiful package! So useful! Thanks a lot for it, @chrisullyott!

It would be great to add a third optional argument to FileSize() so one could choose the desired number delimiters (eg. 2.144.57 KB versus 2.144,57 KB). Something like:

$size = new FileSize(10921134, 10); // Readme example
echo $size->asAuto(); // '10.92 MB'

$size = new FileSize(10921134, 10, true); // This is the FR on this issue
echo $size->asAuto(); // '10,92 MB'

As there's currently no standards ruling decimal separators (!), a simple optional bool argument would suffice and should not break anything for anyone.

Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.