Upload file on pre_update_option_{option_name}

is there a way to upload a file while saving options in WP ?
I have this option setting (save is binded to options.php) and need to upload and store a file on server

Upload file on pre_update_option_{option_name}

and cannot find the file in update_option_{option_name}:

add_action('init', array($this,'upload_credentials'), 10);
function upload_credentials( ) {
  //CRM_adv_settings is the option name
  add_filter( 'pre_update_option_CRM_adv_settings', array($this,'system_save_file'), 10, 2 );
}

function system_save_file($new_value, $old_value){
  //$rawData = file_get_contents("php://input");
  $f=$_FILES;
  $p=$_POST;
}

Cannot find $_FILES, in $_POST there’s the filename; tried several variation about update_option and pre_update_option but no success, any idea?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I’m quite sure the problem is not with the WordPress hook you’re using. Instead, the following:

Cannot find $_FILES, in $_POST, there’s the filename

.. is most likely because your form tag (<form>) does not have the required enctype attribute which must be set to multipart/form-data to make file upload input works in that the file gets uploaded to the server — without setting the enctype to multipart/form-data, the input still works (i.e. you can select a file that you want to upload), but it’ll work like a standard input field where the browser submits only the file name (e.g. my-image.png) and not the actual file itself (which PHP puts in the $_FILES).

So make sure your form tag has the attribute enctype="multipart/form-data":

<form method="post" action="options.php" enctype="multipart/form-data">
    ...
</form>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x