This is the copy of alvincr.com

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:

https://alvincr.com/2021/01/%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e6%98%af%e5%87%ba%e7%8e%b0%e6%97%b6%e5%87%ba%e7%8e%b0%e7%94%b1%e4%ba%8e%e5%ae%89%e5%85%a8%e5%8e%9f%e5%9b%a0%ef%bc%8c%e8%bf%99%e4%b8%aa%e6%96%87%e4%bb%b6/

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.

img

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

img

img

AlvinCR, 2021.1.9 (Supplement):

Using this function, the following error occurs:

img

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');

img

img

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

The whole process of registering and using GitHub Pages

  1. 1. view my personal blog original article for more:
    1. 1.1. 0 background
    2. 1.2.
    3. 1.3. 1 solution
      1. 1.3.1. 1 code
        1. 1.3.1.1. 1.1 Allow all types of uploads
        2. 1.3.1.2. 1.2 Allow certain types of uploads
        3. 1.3.1.3. 1.3 Add specific code
      2. 1.3.2. 2 plugin
      3. 1.3.3. 3 Adjust the format
    4. 1.4. 2 Other reasons and methods
      1. 1.4.1. 2.1 Upload permissions
      2. 1.4.2. 2.2 Other codes