The Daily WTF
Michael was assigned a short, investigatory ticket. You see, their PHP application allowed file uploads. They had a rule: the files should never be larger than 20MB. But someone had uploaded files which were larger. Not much larger, but larger. Michael was tasked with figuring out what was wrong.
Given that the error was less than half a megabyte, Michael had a pretty good guess about why this was.
if (round($uploadedFile->getSize() / 1024 / 1024) > 20) { [ … throw some error message ] }
The developer’s instincts weren’t entirely bad. Take the number of bytes, divide by 1024 twice to get it down to megabytes, and then compare against twenty. It’s probably not how I’d write it, but it’s not wrong– at least not until you start rounding the number off.
Why was the developer rounding in the first place?
“Because 20 is an integer, and I
To read the full article click on the 'post' link at the top.