[wp-hackers] Media upload: limit file type and size

Otto otto at ottodestruct.com
Tue Jan 5 17:02:24 UTC 2010


You can add a filter to 'upload_mimes' to make only certain file types
able to be uploaded. Basically something like this:

add_filter('upload_mimes','demo');

function demo($mimes) {
$mimes = array(
		'jpg|jpeg|jpe' => 'image/jpeg',
		'gif' => 'image/gif',
		'png' => 'image/png',
);
return $mimes;
}

Note that this will only affect users without the unfiltered_upload
capability. Admins and people with unfiltered_upload and upload
anything they want.

Filtering on size would be much more difficult. WordPress has no size
limiting code built in. You could do it by attaching a function to the
wp_handle_upload filter, but it would have a lot of work to do as that
filter is fired after the file is already moved to the uploads
directory (but before the attachment is actually created).

-Otto



On Tue, Jan 5, 2010 at 10:38 AM, Claudio Simeone <mrbrog at gmail.com> wrote:
> Hi all,
>
> is it possible to allow in the media uploader only jpg / gif / png
> files and set a file size limit only for some file types?
>
> I need to disallow some users from uploading very large bmp image
> files, but giving them the possibility to upload videos.
> The best would to set an option per file type, e.g:
>
> - disable bmp upload (and other image types)
> - allow only jpg, gif, png files less than 2 Mb
> - allow only flv files less than 6 Mb.....
>
> Is there a way to do this? In the support forum I found nothing.
>
> thanks in advance
> Claudio
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list