elephant-php/byte-size
Composer 安装命令:
composer require elephant-php/byte-size
包简介
ByteSize uses binary conversion (1024-based) for file-size formatting.
README 文档
README
ByteSize
A small value object for working with file sizes in a clean and predictable way.
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.1or 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 МБ
$precisioncontrols the number of decimal places before trailing zeros are trimmed$labeloverrides 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.
elephant-php/byte-size 适用场景与选型建议
elephant-php/byte-size 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「conversion」 「formatter」 「filesize」 「value-object」 「bytes」 「file-size」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 elephant-php/byte-size 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 elephant-php/byte-size 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 elephant-php/byte-size 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Twig extension to convert filesize to human readable string
Utility component for nicely formatted file sizes
Simple file size conversion for PHP 5.3+
Adds useful definable constants which are missing from the WP Core.
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Yii2 phone formatter and validator.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 50
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-05-11