Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a solution that limits user storage in an app?

Tags:

php

I can't figure out a good solution for limiting the storage amount a user may access with his files.

In the application users are allowed to upload a limit amount of files. The limitation is based on total file size. I.e. a user might be allowed to store 50 Mb on the server.

This number is stored in a database so that it can be easily increased/decreased.

The language used is PHP, but I guess the solution isn't depended on the scripting language.

Very sorry if the question is unclear. I don't really know what to ask for more than a strategy to implement.

Thanks!

like image 631
Christoffer Avatar asked Dec 30 '25 19:12

Christoffer


1 Answers

Keeping track of how much space has been used should be straightforward - with each upload you could store the space used in another table. The PHP filesize() function will tell you the size of a file on disk. Use a SUM() SQL query to get the total size of all the files uploaded by each user, and compare it against their quota limit.

The tricky bit is when you're approaching the limit - you can't tell how big the file is going to be before it's uploaded. So you'll have to get the user to upload a file and then check its size and see if it takes them over quota. If the file's too big, delete and let the user know they're out of space.

like image 103
drewm Avatar answered Jan 02 '26 10:01

drewm