List Info

Thread: note 78405 added to faq.using




note 78405 added to faq.using
user name
2007-10-10 12:31:49
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  -- h
ttps://master.php.net/note/delete/78405/integrated
Del: useless     -- http
s://master.php.net/note/delete/78405/useless
Del: bad code    -- htt
ps://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     -- http
s://master.php.net/note/delete/78405/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78405
Reject      -- https://mast
er.php.net/note/reject/78405
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )