1

I'm trying to upload files with dwg extension to media but when I do that I get an error telling me that uploading files with this extension is not allowed. I tried to upload them via ftp but I cannot see them in my panel afterwards. I've looked for a solution online and tried adding this to my functions.php:

function custom_upload_mimes ( $existing_mimes=array() ) {
    $existing_mimes[‘dwg’] = ‘application/dwg’;
    return $existing_mimes;
}
add_filter(‘upload_mimes’,’custom_upload_mimes’);

But it didn't change anything. Is there any other way to bypass this file restriction?

2
  • 1
    Is your actual code using and rather than '? Commented Jun 25, 2018 at 7:38
  • Initially it was, later I pasted code @Krzysiek Dróżdż suggested but it didn't help. Now I tried using the code from the original code again but with ' instead but it didn't help either.
    – aMJay
    Commented Jun 25, 2018 at 7:41

2 Answers 2

2

Your code should work just fine. The only problem in there is that you’ve set incorrect mime type, I guess...

It should be image/vnd.dwg.

So this one should work:

function custom_upload_mimes ( $existing_mimes=array() ) {
    $existing_mimes['dwg'] = 'image/vnd.dwg';
    return $existing_mimes;
}
add_filter('upload_mimes', 'custom_upload_mimes');
6
  • Still doesn't work, I'm getting same error which says more or less Due to safety reasons this file type is not allowed. I've pasted this at the top of functions.php, maybe I need to put it in a specific place in code?
    – aMJay
    Commented Jun 25, 2018 at 6:48
  • Are you sure that you've added it to your active theme? I've just tested that code and it works fine. You can paste it in functions.php file of your theme. Commented Jun 25, 2018 at 7:29
  • I tried pasting into both availible themes to make sure it was not the problem but it didn't help, I'll try bit more maybe I'm missing something obvious
    – aMJay
    Commented Jun 25, 2018 at 7:36
  • @aMJay where and how exactly are you pasting it? Commented Jun 25, 2018 at 7:43
  • I tried yet another time copying it the way you said and now it works, I have a feeling I've been doing it exactly the same way before but I guess I must have done something wrong earlier, anyway thanks for your help
    – aMJay
    Commented Jun 25, 2018 at 7:46
-1

If it can help: to fix the Due to safety reasons this file type is not allowed error

you need to add this in the wp-config.php :

define('ALLOW_UNFILTERED_UPLOADS', true);

The dwg mimes are multiple, you should add all these:

function custom_upload_mimes ( $existing_mimes=array() ) {
    $existing_mimes['dwg'] = 'image/vnd.dwg';
    $existing_mimes['dwg'] = 'application/acad';
    $existing_mimes['dwg'] = 'application/autocad';
    $existing_mimes['dwg'] = 'application/autocaddwg';
    $existing_mimes['dwg'] = 'application/dwg';
    $existing_mimes['dwg'] = 'drawing/dwg';
    $existing_mimes['dwg'] = 'image/dwg';

    return $existing_mimes;
}
add_filter('upload_mimes', 'custom_upload_mimes');
2
  • It's a security risk to just allow all file uploads with ALLOW_UNFILTERED_UPLOADS.
    – MikeeeG
    Commented Dec 5, 2022 at 12:13
  • Also this will only ever return the last $existing_mimes['dwg'] = 'image/dwg'; as you are redeclaring the same array item each time.
    – MikeeeG
    Commented Dec 5, 2022 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.