I am calculating the size of a file upload in PHP, to format in MB with one decimal place like so:
$metadata['upload_data'] = intval($_FILES['Filedata']['size'] / 104857.6) / 10;
When I echo $metadata['upload_data'] the output is 1.7 as I would expect. But when I serialize the array with serialize($metadata) and save it to a file, the output is:
a:2:{s:7:"uploads";i:11;s:11:"upload_data";d:1.6999999999999999555910790149937383830547332763671875;}
I’m trying to be efficient by storing file sizes in MB not bytes, but this seems worse! Why would PHP store it that way? And am I going about this the right way? Thanks
From the manual: http://php.net/manual/en/language.types.float.php
I would suggest using
json_encodeandjson_decodeif you want to see “1.7” in a serialized version of your array. These functions also end up being quicker thanserializeandunserializeand are also easier to read (by easier to read, I mean a person reading them, rather than the machine).