Jump to content

Calculating size of data (KB) being passed from a form


jjacquay712

Recommended Posts

I am making a website that lets users submit data from their forms to our servers. I want to be able to measure the amount of data being passed to the php script from a form. I know that one character is a byte, so should I just use the strlen() function to count the characters and add the bytes? Is there a better way to do this? Thanks for the help.

I figured out I can use the mb_strlen function. I went ahead and wrote a recursive function for those who might be interested.

 

<?php
function varsize($var) {
$size = 0;
if ( is_array($var) ) {
	foreach ( $var as $key => $value ) {
		if ( is_string($key) ) {
			$size += mb_strlen($key, '8bit');
		}

		if ( is_array($value) ) {
			$size += varsize($value);
		} else {
			$size += mb_strlen((string)$value, '8bit');
		}
	}	
} else {
	$size += mb_strlen((string)$var, '8bit');
}
return $size;
}

echo varsize(array("test"=>array("recursive"=>"cool")));
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.