Using MySQL's ROUND command to report file sizes in Mb

© copyright 04.Jan.2009 by Paul Bradley filed under MySQL


[Ad] need a break from coding?

Hoseasons Villas

I have a database table which stores the file sizes (in bytes) of all files uploaded to a particular website; and needed to report the total values back to the user in Mb values. I looked at doing a function within the PHP application, until I released MySQL's ROUND and CONCAT commands would do the trick perfectly.


SELECT CONCAT(ROUND(
    SUM(file_size)/1024/1024, 2), " Mb")
    AS mb_filesize FROM tbl_mp3_details
    WHERE user_id = 12345;

So if you have a web site where users can upload files, you can easily retrieve the size of the uploaded file by using the PHP filesize function and then store this value in your database. If you then need to report the file size of all the users files, then you can use the SQL syntax above, instead of looping through all the files on disk to get the total size in Mb.

 


If you have found this article helpful or useful please consider linking to it, emailing it to friends, or share it with others using social sites like del.icio.us, Stumble Upon or Twitter.

Paul Bradley

About the Author
Paul Bradley is a VB.NET software developer living and working in Cumbria. He has over 20 years programming experience. He also produces e-learning videos at Code by Example and Linux by Example

Other Popular Articles

Categories & Topics

Home · Apache · JavaScript · Perl · PDF · PHP · MySQL · MSSQL · TAR · Ubuntu Linux · Video · Visual Basic

Browse the complete article history, and if you like what you see; consider subscribing to the rss feed.