How do I create a solution that limits user storage in an application?

I cannot find a good solution for limiting the amount of storage a user can get with their files.

In the application, users are allowed to upload a limited number of files. The limitation is based on the total file size. That is, the user may be allowed to store 50 MB on the server.

This number is stored in the database, so it can be easily increased / decreased.

The language used is PHP, but I think the solution is independent of the scripting language.

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

Thank!

+2


source to share


5 answers


Keeping track of how much space has been used should be simple: on each load, you can save the space used in another table. The PHP function filesize()

will tell you the size of the file on disk. Use a SUM()

SQL query to get the total size of all files uploaded by each user and compare them against their quota limit.



The hard bit is when you're nearing the limit - you can't tell how big the file will be before it's loaded. Thus, you will need to force the user to download the file and then check its size and see if it requires their quota. If the file is too large, delete it and report it out of place.

+4


source


A simple approach would be to store the filename, dates and sizes uploaded by users to the database. Then you can easily refuse to download when it exceeds their total memory.

It also makes it easier to display a list of files sorted in different ways, allowing the user to get closer to their limit to select some files to delete.



You can even use the average size of the files the user uploads to alert them when they are close to using up their entire space.

+2


source


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

+1


source


Drewm's solution is good, I just want to add a few words about the tricky part he mentioned.

Yes, it's impossible to predict the file size before the file is uploaded as you cannot check the file size using javascript on the file upload page. However, you can do this using a flash file uploader (e.g. swfupload.org). Using it, you can check the file size before uploading and check it for upload limitation. This way, you save time for the user (no need to download the file to get the "limit over error" message).

As a bonus, you can also show a user's download progress bar.

0


source


Don't forget about OS decisions. If the files are stored in a special user directory, you can use the OS to find the disk space used in that directory. A Linux solution would be 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";

      

0


source







All Articles