I need to prevent uploading bmp image for user. How can it be possible?
The magic is in get_allowed_mime_types()
which calls the upload_mimes
filter. That is filtering a default array consisting of keys as a non-terminated regular expression of file-extensions and the mapped mime-type as values:
array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'bmp' => 'image/bmp',
'tif|tiff' => 'image/tiff',
'ico' => 'image/x-icon',
....
}
so hooking into that filter and removing bmp
should do the job for the moment:
/** prevent uploading of .bmp files. */
add_filter('upload_mimes', function(array $mimes)
{
unset($mimes['bmp']);
return $mimes;
})
;
Just copy that over into a file named no-bmp-upload.php
and place it into the wp-content\mu-plugins
folder.
-
It may be prudent to mark this as the correct answer first, if it has addressed your question.. :) – t31os Nov 17 '10 at 15:13
-
After adding the code to function.php my site shows blank page. I dont no why. Is there any wrong? – mha Mar 7 '11 at 10:40
-
Please check your error log. You might not be using PHP 5.3, the code above makes use of anonymous functions which is a PHP 5.3 feature. You can just replace the anonymous function with a regular one which should do the trick. – hakre Mar 7 '11 at 11:07
-
Yes Iam not using php 5.3 I have changed the code to <pre><code> add_filter('upload_mimes','prevent_uploading_bmp'); function prevent_uploading_bmp($mimes) { array_key_exists('bmp', $mimes) && unset($mimes['bmp']); return $mimes; } </code></pre> But the same problem. My site goes blank. – mha Mar 7 '11 at 11:49
-
I have found the solution from here. And its working!
WordPress has a set of restricted filetypes it will allow you to upload via the media library. Whilst this is a great security feature, there may be times where you’d like to add other files that are restricted by default, or maybe even the opposite where you’d only like to allow a few extensions to be uploaded. Fortunately, WordPress makes this dead easy with a small snippet of PHP code.
If you’d like to add or remove a specific filetype that can be uploaded to wordpress via the media library, you can insert this PHP code in your theme functions.php file:
function my_myme_types($mime_types){
//Adjust the $mime_types, which is an associative array
//where the key is extension and value is mime type.
return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);
Here is an example of what you can do to add and remove a new filetype (in this example, I’m adding an extension that already exists, but the concept is the same):
function my_myme_types($mime_types){
$mime_types['avi'] = 'video/avi'; //Adding avi extension
unset($mime_types['pdf']); //Removing the pdf extension
return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);
You can also reset the allowed filetypes by creating a new array within the function and returning these values:
function my_myme_types($mime_types){
//Creating a new array will reset the allowed filetypes
$mime_types = array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'bmp' => 'image/bmp',
'tif|tiff' => 'image/tiff'
);
return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);
If you’d like to see what filetypes are currently supported by wordpress, check out the function get_allowed_mime_types located in the wp-includes/functions.php file.