I’m using the Media Library on the front end of my website and I’d like to stop users from being able to spam my server by uploading an unlimited number of files.
As such, I’d like to do one or maybe all of the below:
- Give users a maximum upload capacity; i.e. users can upload up to 10 megabytes of files.
- Limit the number of files a user can upload on a per-post basis
- Limit the number of files a user can upload when they click the “Insert” button, i.e. the Flash uploader and Classic uploader will only let you upload, for instance, 2 files at a time.
None of these are bullet-proof but they’d hopefully make such “spamming” a difficulty.
Thanks in advance,
Assuming that you’re providing upload functionality via WordPress’ native functions, lik
wp_handle_upload
or something more high-level, we come to the conclusion that several hooks are going to be pulled.http://core.trac.wordpress.org/browser/tags/3.3/wp-admin/includes/file.php#L212
The
wp_handle_upload
function would probably be the last native function to touch the file, and would know all the information that is necessary to keep track of.Two hooks inside this function are of interest:
wp_handle_upload
andwp_handle_upload_prefilter
. The latter comes first, this could check against the current limits and prevent the file from being uploaded. The former would track filesizes and count. Storing the information would be handled by none other thanupdate_user_meta
.Theoretically, this works; practically – untested. Let us know how it goes.
Per post upload limits would be kept in the post meta, probably like
{$user_id}_upload_count
etc. Don’t see why that wouldn’t work.If you’re using custom code to handle uploads (which I doublt), then you can implement your own actions and filters just like
wp_handle_uploads
does.I’ve amended Soulseekah’s code somewhat as the
apply_filter
variables weren’t working for me – probably because I don’t understand them!This would be really simple to make a plugin from so I might release it at some point in the future, when I’ve developed an interface for it.