WordPress file upload

Good day. There is a problem: I am creating a WordPress plugin. The functionality of the plugin has the moment of uploading a file to the specified directory when editing a post and, accordingly, attaching the file to the post. Sample code (everything inside a class):

add_action('save_post', array(&$this, 'savePostIcon'));
function savePostIcon($id){
    $opts = $this->getOptions();
    //var_dump($_FILES); - 
    if(isset($_FILES['myplugin'])){
        if(move_uploaded_file($_FILES['myplugin']['tmp_name'], $opts['path'].$_FILES['myplugin']['name'])){
            add_post_meta($id, 'posticon_icon', $_FILES['myplugin']['name']);
        }
    }
}

The file is not sent via $_FILES. And how then to upload it to the directory defined in the plugin settings?
Thanks in advance.

PS: the form does not have enctype. What can you do in this case, besides inserting javascript into your template to load the file?


Answer 1, authority 100%

In general, we need such a form:

<form id="mypluginForm" method="POST" enctype="multipart/form-data">
    <input type="file" name="myplugin">
</form>

If, for example, the form already exists, but there is no way to fix it in the template, enctype can be added using javascript.

onload = function(){
     document.getElementById("mypluginForm").enctype = "multipart/form-data";
}

well, if there is an opportunity to correct the template, we fix it all =)

Then, if the file is uploaded, it will be available in $_FILES['myplugin']


Answer 2, authority 67%

Or in a plugin file

//    multipart/form-data
add_action('post_edit_form_tag' , 'add_form_tag');
function add_form_tag()
{
    echo ' enctype="multipart/form-data"';
}