how to rename file before/after upload I want to rename an up-loaded file. I have the following code. I have defined a submit handler to do so.

function example_form_submit(&amp;$form, FormStateInterface $form_state) {
    if ($form_state-&gt;hasFileElement()) {

        $video_file_file_array = $form_state-&gt;getValue('field_video_file');        
        if (is_array($video_file_file_array)) {
            if (isset($video_file_file_array[0])) {
                $video_file_file_id = $video_file_file_array[0]['fids'][0];
                if($video_file_file_id){
                    $file = \Drupal\file\Entity\File::load($video_file_file_id);
                    $file-&gt;set('filename', 'something');
                    $file-&gt;save();

                }                
            }
        }
    }
}


This is not working. Is there any other way.
Renaming file entity in Drupal 8 can be achieved like this: 

    $file = File::load($file_target_id);
    $new_filename = "filename.png";
    $stream_wrapper = \Drupal::service('file_system')-&gt;uriScheme($file-&gt;getFileUri());
    $new_filename_uri = "{$stream_wrapper}://{$new_filename}";
    file_move($file, $new_filename_uri);


As you can see in this example Drupal file_move function is used here which takes care of everything. You only need to know file target_id which is the file fid.