4

I created a module to upload files from a form. I am able to save the files in the proper folder, but I want to get the name of the uploaded files to save them in the database.

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 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'))));
   }
}
1
  • 1
    If it's a managed_file upload, I think it adds to itself to the database on its own - have you checked the 'file_managed' table in the database to see if your file is appearing as a record in it? Oct 6, 2016 at 14:26

2 Answers 2

5

The 'managed_file' FormElement plugin specifically stores the uploaded file as a managed file entity. This plugin stores the file entity id inside form state value as part of its validation.

  /**
   * {@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'))));
    // Use the fid to load the file entity. Unfortunately ManagedFile loads the file entity, but does not store it in form state.
    $fid = $form_state->getValue('resume');
    $file = \Drupal\file\Entity\File::load($fid);
    // Do something other than "echo" $file->getFilename() here.
  }
2
  • i am able to print $fid but can not get file name
    – gaurav
    Oct 7, 2016 at 10:24
  • Can you edit your question to display the code you are using? You should use methods on the file entity to access the properties such as file name, uri, etc... In fact File::getFilename exists as a public method shorthand, $file->getFilename().
    – mradcliffe
    Oct 7, 2016 at 12:15
1

Piggybacking off of mrradcliffe's method.

$fid = $form_state->getValue('resume');

Then do a DB query

$db = \Drupal::database();
$data = $db->select('file_managed', 'fe')
  ->fields('fe')
  ->orderBy('fe.fid', 'DESC')
  ->range(0, 1)
  ->condition('fe.fid', $fid, '=')
  ->execute();
$value = $data->fetchAssoc();
$filename = $value['filename'];

This should give you the filename, but per your request, it's actually already stored in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.