plugins - Admin page: form with enctype="multipartform-data" does not transfer its data

admin2025-01-08  3

I'm googeling for hours now but can't find anything to my problem:

I'm building a plugin and want to embed a file input in its admin page. For this reason I'm giving the form-element the attribute enctype="multipart/form-data" and I'm trying to access the file via $_FILES, but it is empty! Same with $_POST.

Without enctype="multipart/form-data" at least the filename and the other form fields appear in $_POST.

I'm wondering if WordPress is removing direct access to these superglobals or somehow else preventing this regular way of uploading files.

I've read a lot about hooks and posting all forms to admin_post.php or admin_ajax.php but since my upload functionality is located in a class that's part of a big library with autoloading etc. it's not an option for me. My form is sending it's data to the current admin page (I tried it without the action-Attribute but also specifying it action="admin.php?page=XYZ" but nothing worked).

I would be thankful for every hint!

I'm googeling for hours now but can't find anything to my problem:

I'm building a plugin and want to embed a file input in its admin page. For this reason I'm giving the form-element the attribute enctype="multipart/form-data" and I'm trying to access the file via $_FILES, but it is empty! Same with $_POST.

Without enctype="multipart/form-data" at least the filename and the other form fields appear in $_POST.

I'm wondering if WordPress is removing direct access to these superglobals or somehow else preventing this regular way of uploading files.

I've read a lot about hooks and posting all forms to admin_post.php or admin_ajax.php but since my upload functionality is located in a class that's part of a big library with autoloading etc. it's not an option for me. My form is sending it's data to the current admin page (I tried it without the action-Attribute but also specifying it action="admin.php?page=XYZ" but nothing worked).

I would be thankful for every hint!

Share Improve this question edited Apr 25, 2019 at 11:40 butlerblog 5,0713 gold badges26 silver badges42 bronze badges asked Sep 12, 2017 at 13:57 RobertRobert 3414 silver badges10 bronze badges 1
  • Have you seen this - codex.wordpress.org/Plugin_API/Filter_Reference/… – Summer Developer Commented Aug 2, 2018 at 18:54
Add a comment  | 

1 Answer 1

Reset to default 0

If you want to upload in a custom folder you can use the following function. Add the function in functions.php

function upload_user_file( $file = array(),$path ) {
    if(!empty($file)) 
    {


        $upload_dir=$path;
        $uploaded=move_uploaded_file($file['tmp_name'], $upload_dir.$file['name']);
        if($uploaded) 
        {
            echo "uploaded successfully ";

        }else
        {
            echo "some error in upload " ;print_r($file['error']);  
        }
    }

}

Make a form in your template file like this:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" name="upload">
</form>

Call the upload function in template file like before get_header() function:

if(isset($_POST['upload']))
    {

       if( ! empty( $_FILES ) ) 
       {
          $file=$_FILES['file'];   // file array
          $upload_dir=wp_upload_dir();
          $path=$upload_dir['basedir'].'/myuploads/';  //upload dir.
          if(!is_dir($path)) { mkdir($path); }
          $attachment_id = upload_user_file( $file ,$path);

       }
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266739a1144.html

最新回复(0)