convert file sizes like "10M"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

convert file sizes like "10M"

Post by djot »

-
Hi,


Is there a function that may convert file size information like "10M" to the according size in bytes?



besides:
My uploads fail, because I do set the hidden field with a value I get via ini_get('upload_max_filesize'). ini_get returns "10M" instead of 10485760 (bytes).

Code: Select all

$form = '<input type="hidden" name="MAX_FILE_SIZE" value="'.ini_get('upload_max_filesize').'" />';

djot
-
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You could always enter the value manually, or if the php.ini is always going to have the value in Megabytes:

Code: Select all

<?php

function meg2byte ($val) {
    return ((intval($val) * 1024) * 1024);
}

echo meg2byte('10M');

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

well, it has been discussed in the manual: http://us2.php.net/manual/en/function.i ... #AEN136763
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Thanks,

I only did read all "upload" related posts on php.net. Thanks. Anyway I think php should recognize "10M" as the right size in byte itself, rather than I have to convert it. And no, I do not know if php.ini returns always "10M" because I don't know all the servers the script runs on for sure.


djot
-
Post Reply