note 73411 added to function.ini-get

From: Date: Thu, 22 Feb 2007 02:20:00 +0000
Subject: note 73411 added to function.ini-get
Groups: php.notes 
Request: Send a blank email to php-notes+get-123699@lists.php.net to get a copy of this message
Re Webmaster at Grimchild's code As petrov says, grimchild's code doesn't "parse" the value, it just throws away the most significant part of it. Out of there you'll get a number but won't know whether it's bytes, kilobytes, megabytes or gigabytes! To parse the value you have to split off the character (if present) and multiply the number appropriately. Trivially: <?php $pms = ini_get('post_max_size'); if (preg_match('/^([\d\.]+)([gmk])?$/i', $pms, $m)) { $value = $m[1]; if (isset($m[2])) { switch(strtolower($m[2])) { case 'g': $value *= 1024; # fallthrough case 'm': $value *= 1024; # fallthrough case 'k': $value *= 1024; break; default: die("Can't parse value '$pms' "); } } print "$value bytes\n"; } else { die("Can't parse value '$pms' "); } ?> Note the cunning or horrible (depending on your viewpoint) use of fallthrough between cases in the switch statement, so that for example 'g' gets multiplied by 1024 three times. If you ever write code which intentionally uses switch fallthrough like the above, be a good boy/girl and comment it, else someday it will come and bite you or your unfortunate cow-orkers. It wouldn't surprise me if there's a PHP function to parse human-format sizes like this already (given that PHP itself has to do it). ---- Server IP: 69.147.83.197 Probable Submitter: 217.169.15.38 ---- Manual Page -- http://www.php.net/manual/en/function.ini-get.php Edit -- https://master.php.net/note/edit/73411 Del: integrated -- https://master.php.net/note/delete/73411/integrated Del: useless -- https://master.php.net/note/delete/73411/useless Del: bad code -- https://master.php.net/note/delete/73411/bad+code Del: spam -- https://master.php.net/note/delete/73411/spam Del: non-english -- https://master.php.net/note/delete/73411/non-english Del: in docs -- https://master.php.net/note/delete/73411/in+docs Del: other reasons-- https://master.php.net/note/delete/73411 Reject -- https://master.php.net/note/reject/73411 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123699) next »