I am having one requirement to upload a file and store it in that module folder itself. I am just wondering how to do that. I have created a custom module with File type form element in that as below.

    '#type' => 'managed_file',
    '#title' => $this->t('Certificate'),
    '#upload_validators'  => [
                'file_validate_extensions' => array('pem'),
      '#description' => $this->t('Your Certificate (.pem file)').': '.\Drupal::state()->get('BUCKAROO_CERTIFICATE_NAME'),
      '#default_value' => $config->get('BUCKAROO_CERTIFICATE'),
      '#upload_location' => 'public://myfile/',

Here,I am giving #upload_location as public://myfile. this is working fine by uploading a file in sites/default/files/myfile folder. How can I upload to a custom module folder. Please suggest some alternatives.

  • 2
    You really shouldn't store it in a module folder. It's very bad practice, module folders should not be writable by the web server. Definitely don't store certificates there. That's a nightmare waiting to happen. You should store them in the private file system, and put the private file system outside of the web root to get the best chance at security – Clive Jul 12 '17 at 13:09
  • This should not be in the application at all. I would create a location in the directory, outside of the webroot, like keys or certs, and then tell the code where to locate that file. Also, why mix the use of state() and $config? – Kevin Jul 12 '17 at 16:00
  • I am new to D8, if am wrong, correct me. mixup of state and config because, state which i have mentioned is not a form element. Its inside submit function I am setting a variable called as BUCKAROO_CERTIFICATE_NAME. So only i thought its not a config, its a variable and I am getting value of that variable via state. – Suraj Jul 13 '17 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.