Restrict file uploads by extension?

Is there a way in WordPress 3.1 to restrict allowed file uploads by extension (images only) and file size?

Bonus question: Can I limit users to only be able view files they have uploaded themselves?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I believe you can add a filter to upload_mimes to restrict to certain types.
The hook: http://adambrown.info/p/wp_hooks/hook/upload_mimes

The filter:

add_filter('upload_mimes','restict_mime'); 

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

From what I understand this will not work for admins or any user with the unfiltered_upload
capability.
Also have a look at this post Limit image upload to one and disable audio, video and other document file types to upload

An alternative to control size limit would be to use .htaccess or php.ini:

For upload limit you can set in .htaccess one of the following;

LimitRequestBody 1073741824  //( 1MB)
php_value upload_max_filesize 1M
php_value post_max_size 1M

For php.ini you can set upload_max_filesize = 1M


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x