Answers.Body: <p>I just created a custom extension that uploads a  CSV file and reads it into a table.</p>
  
  <p>I added the file element in <code>buildQuickForm</code> as follows</p>
  
  <pre><code>  $this-&gt;add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
  </code></pre>
  
  <p>And the processing of the file was is done  with</p>
  
  <pre><code>public function preProcess()
  {
      if (isset($this-&gt;_submitFiles['uploadFile'])) {
          $uploadFile = $this-&gt;_submitFiles['uploadFile'];
          $importer = new CRM_Importer();
          $importer -&gt;truncate();
          $importer -&gt; importCVStoTable($uploadFile['tmp_name']);
      }
  }
  </code></pre>
  
  <p>I think the key element is <code>$this-&gt;_submitFiles</code> and you already found about the <code>tmp_name</code>  </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-01-18T16:02:04.300Z
Answers.LastActivityDate: 2018-01-18T16:02:04.300Z
Answers.UserDisplayName: Kainuk - CiviCooP
Body: <p><code>civix generate:form</code> creates a basic form with a <code>buildQuickForm()</code> and <code>postProcess()</code> demonstrating a select option.</p>
  
  <p>I want the form to upload a file and save it to a custom field.</p>
  
  <p>If I add: <code>$this-&gt;add('file', 'file_id', "My document")</code> then I get a normal file upload element on the form, but the problem is what to do with it in the postProcess.</p>
  
  <p><code>$this-&gt;controller-&gt;exportValues()</code> does not include my <code>file_id</code> although it is listed in <code>$this-&gt;getSubmitValues(TRUE)</code> with the the original file name and tmp_name.</p>
  
  <p>I understand QuickForm does some filtering of submitted values but how do I stop the file details being filtered out and get it saved as a custom field?</p>
  
Tags: <quickform>
Title: How do I upload files in a custom form?
ThreadText: # How do I upload files in a custom form?
  
  (Asked by: username_0 on 2018-01-18)
  
  `civix generate:form` creates a basic form with a `buildQuickForm()` and `postProcess()` demonstrating a select option.
  
  I want the form to upload a file and save it to a custom field.
  
  If I add: `$this->add('file', 'file_id', "My document")` then I get a normal file upload element on the form, but the problem is what to do with it in the postProcess.
  
  `$this->controller->exportValues()` does not include my `file_id` although it is listed in `$this->getSubmitValues(TRUE)` with the the original file name and tmp\_name.
  
  I understand QuickForm does some filtering of submitted values but how do I stop the file details being filtered out and get it saved as a custom field?
  
  **Tags:** quickform
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-01-18. Score: 4
  
  I just created a custom extension that uploads a CSV file and reads it into a table.
  
  I added the file element in `buildQuickForm` as follows
  
  ```
    $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
  
  ```
  
  And the processing of the file was is done with
  
  ```
  public function preProcess()
  {
      if (isset($this->_submitFiles['uploadFile'])) {
          $uploadFile = $this->_submitFiles['uploadFile'];
          $importer = new CRM_Importer();
          $importer ->truncate();
          $importer -> importCVStoTable($uploadFile['tmp_name']);
      }
  }
  
  ```
  
  I think the key element is `$this->_submitFiles` and you already found about the `tmp_name`
  
  
  ---
  
  