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?
1 Answer
Reset to default 1I'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>
$_FILE
global in there – Andrea Somovigo Commented Jul 1, 2020 at 14:53enctype
attribute -enctype="multipart/form-data"
? – Sally CJ Commented Jul 1, 2020 at 18:06