Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing File Sizes in a Database

I am currently working on a system that involves storing multiple studies and details of their contents (A study can typically contain 1 < X < ~2000 images). My colleagues and I were discussing what might be the best method of storing files sizes (specifically the image sizes) in a database would be.

The file sizes typically range from < 1kB to > 20MB.

We are currently debating between storing the images sizes as:

# of kilobytes (as an integer value) 
# of bytes (as a large integer value)
# of megabytes (possibly as a decimal value)
Other Options...

I haven't worked with storing file sizes much and was wondering what might be the most efficient / practical method of accomplishing this?

like image 624
Rion Williams Avatar asked Jan 06 '11 16:01

Rion Williams


People also ask

What is a file size in database?

Go Up to Database Files. InterBase database file size is the product of the number of database pages times the page size. The minimum page size is 1 KB, the default page size is 4KB, and the maximum page size is 16KB. Each page can store records only from a single table.

Can we store files in a database?

To save a file in a database, it often needs to be converted in a way so it can be correctly stored. If you choose to store the file as text, you might decide to store it in base64 format for example. You'll need to write some logic in your application to convert files to base64 before they're saved into the database.


2 Answers

If you're going to explicitly store the size at all, store the number of bytes. There is just too much confusion/ambiguity when using other units.

Example: different people might interpret kb as:

  • kilobytes
  • kilobits
  • kibibytes
  • kibibits

...and how big is a kilobyte, anyway?

That said, if you're storing the actual data in your database, I do not see an immediately compelling reason to explicitly store the length of the data at all.

like image 70
Matt Ball Avatar answered Nov 25 '22 12:11

Matt Ball


I store filesizes in bytes as an integer in the database. The (signed) integer field of mysql has a maximum value of 2147483647 so filesizes up to 2GB can be stored without a problem.

like image 42
Stefaan Colman Avatar answered Nov 25 '22 12:11

Stefaan Colman