Zend Framework Hidden Gems – Zend_Measure

Just had a bit of a problem today dealing with displaying file sizes of uploaded reports in an application we are upgrading using Zend Framework. When the files are uploaded, the file sizes are stored in bytes, but for display we need to show the sized in KB as the typical file size is between 60k and 2,048KB (2MB).

Looking through http://php.net I came across some scripts that we could use, but I was looking for a versatile re-useable solution. Well that is when I came across Zend_Measure (http://www.framework.zend.com/manual/en/zend.measure.html).

How did we use it, well, very simply:

  1. Added Zend_Measure library files into our application library folder (we do not install all components by default, we add them as we need to)
  2. Created a new Zend_Measure_Binary (http://www.framework.zend.com/manual/en/zend.measure.types.html) class instance
  3. Add the filesize in bytes
  4. Change the type from bytes to kilobytes (the conversion types are available as constants)
  5. Call the toString() method to output the formatted type

Now we have a reusable element which can be used …

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: