note 78405 added to faq.using
| From: | greenie2600 at yahoo dot com | Date: | Wed, 10 Oct 2007 17:31:49 +0000 |
| Subject: | note 78405 added to faq.using | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131277@lists.php.net to get a copy of this message | ||
Here's a simple function that will convert the shorthand values described in point 64.16 to a
number of bytes.
I use this to display the maximum size of file uploads to the user, so they don't waste time
uploading a huge file only to find that it's too big. (I've been unable to find any
browsers that actually support the MAX_FILE_SIZE technique described in chapter 38, and it's
certainly not part of any W3C spec, so this is the next best thing.)
Here's how you'd use my function for that purpose (though you might want to abstract this
to a function of its own):
<?php
echo 'Maximum file size: ' . convertBytes( ini_get( 'upload_max_filesize' ) ) /
1048576 . 'MB';
?>
And here's the function:
<?php
/**
* Convert a shorthand byte value from a PHP configuration directive to an integer value
* @param string $value
* @return int
*/
function convertBytes( $value ) {
if ( is_numeric( $value ) ) {
return $value;
} else {
$value_length = strlen( $value );
$qty = substr( $value, 0, $value_length - 1 );
$unit = strtolower( substr( $value, $value_length - 1 ) );
switch ( $unit ) {
case 'k':
$qty *= 1024;
break;
case 'm':
$qty *= 1048576;
break;
case 'g':
$qty *= 1073741824;
break;
}
return $qty;
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 66.240.6.124
----
Manual Page -- http://www.php.net/manual/en/faq.using.php
Edit -- https://master.php.net/note/edit/78405
Del: integrated -- https://master.php.net/note/delete/78405/integrated
Del: useless -- https://master.php.net/note/delete/78405/useless
Del: bad code -- https://master.php.net/note/delete/78405/bad+code
Del: spam -- https://master.php.net/note/delete/78405/spam
Del: non-english -- https://master.php.net/note/delete/78405/non-english
Del: in docs -- https://master.php.net/note/delete/78405/in+docs
Del: other reasons-- https://master.php.net/note/delete/78405
Reject -- https://master.php.net/note/reject/78405
Search -- https://master.php.net/manage/user-notes.php