How can I upload files with the form element type file? I want to upload a file and I have to use form element type as 'file'. In submit function I am not able to get any proper data related to the file which I am uploading. SO Can anyone please help me to resolve this. Please find the below code:
public function buildForm(array $form, FormStateInterface $form_state) {
  $form['test_CERTIFICATE'] = [
    '#type' =&gt; 'file',
    '#title' =&gt; $this-&gt;t('Certificate'),
    '#description' =&gt; $this-&gt;t('Your Certificate (.pem file)').': '.\Drupal::state()-&gt;get('test_CERTIFICATE_NAME'),           
  ];
  
  return parent::buildForm($form, $form_state);
}

Submit function:
public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);

  $validators = array('file_validate_extensions' =&gt; array('pem'));
  $files = file_save_upload('test_CERTIFICATE', $validators, 'public://certfiles', FILE_EXISTS_REPLACE);
  $file = File::load($files[0]);
  if ($file) {
    kint($files); exit;
    // here control is not coming
    $file-&gt;setPermanent();
    $file-&gt;save();
  }
}
I had the same problem with the file form element. Solved this by using managed_file and providing the upload location and validators in the form element:

$form['test_CERTIFICATE'] = [
  '#type' =&gt; 'managed_file',
  '#title' =&gt; $this-&gt;t('Certificate'),
  '#upload_location' =&gt; 'private://certfiles',
  '#upload_validators' =&gt; [
    'file_validate_extensions' =&gt; ['pem'],
  ],
];


Then in submit:

use Drupal\file\Entity\File;

$form_file = $form_state-&gt;getValue('test_CERTIFICATE', 0);
if (isset($form_file[0]) &amp;&amp; !empty($form_file[0])) {
  $file = File::load($form_file[0]);
  $file-&gt;setPermanent();
  $file-&gt;save();
}