tags:

views:

68

answers:

5

Hi,

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!

+2  A: 

A simple approach would be to store the filename, dates and sizes of a users uploads in the database too. Then you can easily reject an upload when it exceeds their total storage.

This also makes it easy to show a list of files sorted in a variety of ways, allowing a user close to their limit to select some files for removal.

You could even use the average size of the files the user uploads to warn them when they are getting close to using up all their space.

Paul Dixon
Interesting idea. Might do a variation of that one.
Christoffer
+1  A: 

You can use a script (something like that) that iterates through a directory contents, calculates filesizes and then deletes files that don't fit or rejects new uploads. But I think that this better be done with some sort of directory restrictions on a server. Unfortunately, I'm not a linux guy, so I don't know exactly how to do that, but this post might be helpful.

n1313
+2  A: 

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.

drewm
I will use a variation of the three. But this will be the main solution.
Christoffer
For some user experience, you could also have some javascript that calculates the file size to be uploaded, and if it's bigger than the allotted amount, reject it before it hits your servers. You'll still need the server side code, but you might limit your users frustration after waiting so long for a file to upload only to be told they're out of space.
Josh Smeaton
A: 

Solution of drewm is good, I just want to add few words about tricky part he mentioned.

Yes, it is impossible to predict file size before file is uploaded, as you cannot check filesize using javascript on user`s file upload page. However you can do it using flash based file uploader (swfupload.org for example). By using it you can check files size before upload is started and check it against upload limit you have. This way you will save time for user (no need to upload file to get "limit exceed error" message).

As a bonus you can show user upload progress bar as well.

Anton
A: 

Don' forget about OS solutions. If the files are stored in a "user" specific directory, then you can use the OS to find the disk spaced used in that directory. A Linux solution would something like this:

$dirSize = explode("\t", `du -ks $userDir`);  // Will return an array of size, dirName
if ($dirSize[0] > MAX_DIR_LIMIT) print "USER IS OVER QUOTA";
ChronoFish
I've found that tracking files in BOTH and OS and a DATABASE long term lead to issues. At some point a script will fail halfway and you'll end up with files in a directory without the database reflecting the change. Or worse you'll want to move the files to a new partition/directory structure and now you'll have to update the database. I try to avoid data keeping track of data. The OS will always be right - the DB *might* be right. The alternative is to keep the files as "blobs" in the database - then the OS has no play.
ChronoFish