Simpler filesize calculations

David Carr

Tutorials PHP & MySQL

When working with file sizes in PHP you work with bytes, which often is not what you want. In most cases I prefer to work with megabytes rather the bytes, I came across a snippet on Stackoverflow that solved this issue in a great way.

First, define the different sizes:

define('KB', 1024);
define('MB', 1048576);
define('GB', 1073741824);
define('TB', 1099511627776);

Then you can use them in if statements calculations ie to only allow images less than 20MB:

if ($size < 20*MB) {

Super handy!

Laravel Modules Your Logo Your Logo Your Logo

Become a sponsor

Help support the blog so that I can continue creating new content!

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.