DEV Community

Cover image for πŸš€ Supercharge Your PHP Projects with FileSizeHandler: A Flexible and Extensible File Size Utility
A.S Nasseri
A.S Nasseri

Posted on

πŸš€ Supercharge Your PHP Projects with FileSizeHandler: A Flexible and Extensible File Size Utility

Are you constantly dealing with files in your PHP projects and struggling with size calculations, storage inconsistencies, or integration with cloud platforms? Say goodbye to the hassle! Introducing FileSizeHandler, a lightweight, flexible, and extensible PHP utility that handles file sizes like a pro. Whether you're working with local files, remote URLs, or cloud storage, this library has got you covered.

In this post, we’ll explore what makes FileSizeHandler a must-have library for your PHP projects and how to leverage its powerful features.

πŸ”‘ Why You Need FileSizeHandler

Handling file sizes is more than just reading bytes. Different systems measure file sizes differently:

  • Binary vs. Decimal Units: OS file systems use binary units (MiB, GiB), while storage manufacturers use decimal (MB, GB). Confusing, right?
  • Cross-Platform Challenges: File size interpretations vary across operating systems and storage platforms.
  • Cloud and Remote Files: Measuring file sizes from FTP, S3, or Google Cloud often requires additional work or APIs.

FileSizeHandler simplifies all of this by providing a unified, fluent API for file size calculations, formatting, and integrations.

✨ Features That Make It Shine

  1. Local, Remote, FTP, and Cloud Support Fetch file sizes seamlessly from:
  • Local file systems
  • Remote HTTP/HTTPS URLs
  • FTP servers
  • Amazon S3, Google Cloud Storage, and more!
  1. Binary and Decimal Unit Support
    Switch between binary (1024-based) and decimal (1000-based) units
    with a single method.

  2. Fluent Interface
    Chain methods to create clean and readable code.

  3. Localization
    Format file sizes with localized unit names (e.g., Mio, Kio) for a
    better user experience.

  4. Extensibility
    Easily extend the library with custom sources for specialized file
    size calculations.

πŸ› οΈ Installation

Install the library using Composer:

composer require nassiry/filesize-handler  
Enter fullscreen mode Exit fullscreen mode

πŸš€ Getting Started

Local File Example
Let’s start with the basics. Fetch the size of a local file in binary units (default):

use Nassiry\FileSizeUtility\FileSizeHandler;  

$handler = FileSizeHandler::create()  
    ->local('/path/to/your/file.txt')  
    ->baseBinary();  

echo $handler->formattedSize(); // Output: "1.23 MiB"  
Enter fullscreen mode Exit fullscreen mode

Switch to decimal units with just one method:

echo $handler->baseDecimal()->formattedSize(); // Output: "1.30 MB"  
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Why Developers Love FileSizeHandler

πŸ”§ Simplifies File Size Handling
No more juggling different APIs and libraries for local, remote, or cloud file sizes.

🌍 Extensible and Future-Proof

Adding new storage platforms is as simple as creating an extension or implementing an interface.

⚑ Lightweight and Performant

Built for speed and simplicity, with zero unnecessary bloat.

πŸ“š Learn More

Check out the GitHub Repository for more details, documentation, and examples. and give a STAR if you like it.

Introducing Extensions

The power of FileSizeHandler doesn’t end with the core library. Take your file size handling to the next level with official extensions:
Want to see official extensions in action? Explore them below:

FTP Extension
S3 Extension
Google Cloud Extension
Remote URL Extension

πŸŽ‰ Get Started Today

With FileSizeHandler, handling file sizes in PHP has never been easier. Install it, extend it, and make your projects smarter and more efficient.

Happy coding! πŸš€

Top comments (0)