最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

options - Upload file on pre_update_option_{option_name}

programmeradmin3浏览0评论

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

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?

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

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?

Share Improve this question asked Jul 1, 2020 at 6:39 Andrea SomovigoAndrea Somovigo 1,6151 gold badge10 silver badges14 bronze badges 4
  • "save is binded to options.php" - so you're using the settings API? – Sally CJ Commented Jul 1, 2020 at 13:39
  • yes, the 'save' pass in my function system_save_file() but I don't find the $_FILE global in there – Andrea Somovigo Commented Jul 1, 2020 at 14:53
  • Does your form tag have the enctype attribute - enctype="multipart/form-data"? – Sally CJ Commented Jul 1, 2020 at 18:06
  • 1 thanks a lot, that was ! if you want to answer I'll accept it – Andrea Somovigo Commented Jul 1, 2020 at 22:15
Add a comment  | 

1 Answer 1

Reset to default 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>
发布评论

评论列表(0)

  1. 暂无评论