I made a module to upload file in form, below is my code snippet, I am able to save file in proper folder but I want to get name of file uploaded so I can save it in database and make reference for it, I tried lots of thing but was not able to do it.
class MyForm extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'my_forms'; } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $form['resume'] = array( '#type' => 'managed_file', '#title' => t('File'), '#required' => TRUE, '#upload_validators' => array( 'file_validate_extensions' => array('pdf doc docx'), 'file_validate_size' => array(25600000), ), '#upload_location' => 'public://myfile/', ); $form['actions']['#type'] = 'actions'; $form['actions']['submit'] = array( '#type' => 'submit', '#value' => $this->t('Submit'), '#button_type' => 'primary', ); return $form; } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { drupal_set_message($this->t('@can_name ,Your application is being submitted!', array('@can_name' => $form_state->getValue('name')))); // echo file name here } }