When the file is uploaded, a solution appears as "This file type is not supported for security reasons"
view my personal blog original article for more:
0 background
When I was analyzing and sorting out the code, I uploaded a file but the phenomenon of “Sorry, this file type is not supported for security reasons.” appeared. Through personal testing, I summarized the following methods so that everyone can quickly solve the same problem.
1 solution
1 code
1.1 Allow all types of uploads
Put the following code in wp-config.php to directly solve this problem.
define(‘ALLOW_UNFILTERED_UPLOADS’, true); //Allow uploading various types of files
AlvinCR, 2021.1.9 (Supplement):
Using this function, the following error occurs:
This is caused by a code input error, please recheck the correctness of the code. If copying and pasting the code above is wrong (caused by the input method), you can also manually enter it by referring to the code in the picture.
1.2 Allow certain types of uploads
The following is a code snippet, but when I use this code, an exception occurs (the server receives an unexpected response, and a fatal error causes the website to crash), maybe it is a problem with my settings.
function my_custom_upload_mimes($mimes = array()) {
// Add a key and value for the SVG file type
$mimes['svg'] = “text/txt”; //Replace txt here with the type you want to open
return $mimes;
}
add_action('upload_mimes', 'my_custom_upload_mimes');
1.3 Add specific code
Since there is no application function in my function.php file, the following content is for reference only, and the effect is unknown:
Wordpress adds custom upload attachment type and rar support
Find application/zip in the wp-includes/functions.php file, and add it above the line “// openoffice formats”
‘rar’ =>’application/rar’,
2 plugin
3 Adjust the format
When uploading, you can directly change the suffix of rar to jpg, and then manually change it back after uploading
2 Other reasons and methods
2.1 Upload permissions
It may also be because there is no open upload permission, you can find
/www/wwwroot/alvincr.com/wp-content/uploads
Open all the permissions of the uploads folder, which is set to 777
2.2 Other codes
add_filter(‘upload_mimes’,’custom_upload_mimes’);
function custom_upload_mimes ($existing_mimes=array())
{
// Add file extension’extension’ with mime type’mime/type’
$existing_mimes[‘extension’] =’mime/type’;
// add as many as you like eg
$existing_mimes[‘rar’] =’application/rar’; //Add rar type files
// remove items here if desired…
//unset( $existing_mimes[‘exe’] );
// and return the new full result
return $existing_mimes;
}
https://www.eee-eee.com/blog-news/90-wordpress/1174-wordpress-permissions.html