How to calculate file size in php?
1 min readMay 26, 2020
It’s so easy … you need to know that the count is in bytes (B).
Now we go to calculate:
1 KB = 1.024 B
1 MB = 1.024 KB = 1.048.576 B
1 GB = 1.024 MB = 1.048.576 KB = 1.073.741.824 B
1 TB = 1.024 GB = 1.048.576 MB = 1.073.741.824 KB = 1.099.511.627.776 B
The most simple form to you calculate in your PHP code is defined constants with type of sizes. Ex:
define('KB', 1024);
define('MB', 1048576);
define('GB', 1073741824);
define('TB', 1099511627776);
Now it’s easy.
Ex 1. If you want a file less to 10MB:
if ($_FILES['file']['size'] < 10*MB)
Ex 2. If you want a file less to 1GB:
if ($_FILES['file']['size'] < GB)