Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Easily calculate file sizes and convert between units.

License

NotificationsYou must be signed in to change notification settings

chrisullyott/php-filesize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable VersionTotal Downloads

php-filesize

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

Installation

Include in your project, or, install withComposer:

$ composer require chrisullyott/php-filesize

Instantiate

AFileSize 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.

useChrisUllyott\FileSize;$size =newFileSize('500 GB');

Convert between units

Useas() 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 is2).

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

User-friendly formatting

UseasAuto() to get a user-friendly string:

$size =newFileSize('1234522678.12 KB');echo$size->asAuto();// '1.15 TB'

Optionally,asAuto() also provides a decimal precision.

$size =newFileSize('1234522678.12 KB');echo$size->asAuto(5);// '1.14974 TB'

Or, simplyecho the object for the same functionality:

echo$size;// '1.15 TB'

Modify the size

To make changes, useadd(),subtract(),multiplyBy(), anddivideBy().

$size =newFileSize('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 useadd() andsubtract() 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 either2 (binary) or10 (decimal). We use binary by default. To handle sizes in decimal:

$size =newFileSize(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 =newFileSize('1.234.522.678,12 KB',2,',');echo$size;// '1,15 TB'

About

Easily calculate file sizes and convert between units.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp