© copyright 04.Jan.2009 by Paul Bradley filed under MySQL
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.
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