elephant-php/byte-size 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

elephant-php/byte-size

最新稳定版本:0.2.0

Composer 安装命令:

composer require elephant-php/byte-size

包简介

ByteSize uses binary conversion (1024-based) for file-size formatting.

README 文档

README

elephant-php

ByteSize

A small value object for working with file sizes in a clean and predictable way.

Latest Version License PHP 8.1+


It uses 1024 based conversion and gives you a simple API to:

  • create sizes from bytes, KB, MB, GB, or TB
  • convert values between units
  • format values for UI or CLI output
  • generate human-readable file size strings

Installation

composer require elephant-php/byte-size

Requirements

  • PHP 8.1 or higher

Quick example

use ElephantPhp\ByteSize\ByteSize;
use ElephantPhp\ByteSize\ByteSizeUnit;

$size = ByteSize::fromBytes(2_621_440);

echo $size->toMegabytes();                      // 2.5
echo $size->format(ByteSizeUnit::MEGABYTES);    // 2.5 MB
echo $size->human();                            // 2.5 MB
echo $size->human(0);                           // 2 MB
echo $size->human(0, 'Megabytes');              // 2 Megabytes
echo (string) $size;                            // 2.5 MB

Real-world example

use ElephantPhp\ByteSize\ByteSize;

$bytes = filesize('/path/to/archive.zip');
$size = ByteSize::fromBytes($bytes);

echo $size->human(); // e.g. "14.8 MB"

Creation

use ElephantPhp\ByteSize\ByteSize;

ByteSize::fromBytes(2621440);
ByteSize::fromKilobytes(2560);
ByteSize::fromMegabytes(2.5);
ByteSize::fromGigabytes(0.0025);
ByteSize::fromTerabytes(0.0000025);

Short aliases are available too:

ByteSize::bytes(2621440);
ByteSize::kb(2560);
ByteSize::mb(2.5);
ByteSize::gb(0.0025);
ByteSize::tb(0.0000025);

Conversion

use ElephantPhp\ByteSize\ByteSize;

$size = ByteSize::fromBytes(2_621_440);

$size->toBytes();      // 2621440
$size->toKilobytes();  // 2560
$size->toMegabytes();  // 2.5
$size->toGigabytes();  // 0.00244140625
$size->toTerabytes();  // 0.000002384185791015625

Short aliases:

$size->toKb();
$size->toMb();
$size->toGb();
$size->toTb();

Formatting

Use format() when you want an explicit unit.

use ElephantPhp\ByteSize\ByteSize;
use ElephantPhp\ByteSize\ByteSizeUnit;

$size = ByteSize::fromBytes(2_621_440);

echo $size->format(ByteSizeUnit::TERABYTES, 7);       // 0.0000024 TB
echo $size->format(ByteSizeUnit::TERABYTES, 7, '');   // 0.0000024
echo $size->format(ByteSizeUnit::MEGABYTES, 2);       // 2.5 MB
echo $size->format(ByteSizeUnit::MEGABYTES, 2, 'МБ'); // 2.5 МБ
  • $precision controls the number of decimal places before trailing zeros are trimmed
  • $label overrides the default unit label
  • passing '' as label returns only the numeric value

Human-readable output

Use human() when you want ByteSize to choose the most suitable unit automatically.

use ElephantPhp\ByteSize\ByteSize;

$size = ByteSize::fromBytes(2_621_440);

echo $size->human();              // 2.5 MB
echo $size->human(2, 'MEGABYTE'); // 2.5 MEGABYTE
echo (string) $size;              // 2.5 MB

Examples:

ByteSize::fromBytes(999)->human();                 // 999 B
ByteSize::fromBytes(1024)->human();                // 1 KB
ByteSize::fromBytes(1048576)->human();             // 1 MB
ByteSize::fromBytes(1073741824)->human();          // 1 GB
ByteSize::fromBytes(1099511627776)->human();       // 1 TB

Available units

use ElephantPhp\ByteSize\ByteSizeUnit;

ByteSizeUnit::BYTES;
ByteSizeUnit::KILOBYTES;
ByteSizeUnit::MEGABYTES;
ByteSizeUnit::GIGABYTES;
ByteSizeUnit::TERABYTES;

License

MIT License

Please see LICENSE for more information.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固