Answers.Body: <p>You need to register the file into the assets.
  We can do like this :</p>
  <pre><code>        $myFile = UploadedFile::getInstanceByName('cadFile');
          $success = $myFile-&gt;saveAs('assets/order/' . $myFile-&gt;baseName . '.' . $myFile-&gt;extension);
          $asset                          = new Asset();
          $asset-&gt;tempFilePath            = 'assets/order/' . $myFile-&gt;baseName . '.' . $myFile-&gt;extension;
          $asset-&gt;filename                = $myFile-&gt;baseName . '.' . $myFile-&gt;extension;
          $asset-&gt;volumeId                = 1;
          $asset-&gt;setScenario(Asset::SCENARIO_CREATE);
          $result = Craft::$app-&gt;getElements()-&gt;saveElement($asset);
  </code></pre>
  <p>So that code will upload your code and register it as asset, for the volumeId you can replace it with your volume id.
  Then after that we need to save the assets in your entry. To save it on your field you need to pass your asset id inside array.</p>
  <pre><code> entry-&gt;setFieldValue('cadFile', [asset-&gt;id]);
   $success = Craft::$app-&gt;elements-&gt;saveElement($entry, false);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-27T02:01:24.860Z
Answers.LastActivityDate: 2021-10-27T02:01:24.860Z
Answers.UserDisplayName: aodihis
Body: <p>I want a user to upload a PDF file to his request. On the backend, I can manage to save the file on server, but I'm looking for a way to save it in the Entry just as if I was doing it manually on the admin panel but don't know how.
  So far I tried different things but I'm not sure what goes in the &quot;cadFile&quot; field as a handle to the actual file. Any help would be appreciated.</p>
  <pre><code>if($_FILES){
    $myFile = UploadedFile::getInstanceByName('cadFile');
    $success = $myFile-&gt;saveAs('assets/order/' . $myFile-&gt;baseName . '.' . $myFile-&gt;extension);
  
    if($success) {
      $model-&gt;setFieldValue('cadFile', [$myFile-&gt;baseName . '.' . $myFile-&gt;extension]);
      $success = Craft::$app-&gt;elements-&gt;saveElement($model, false);
  
  </code></pre>
  
Tags: <assets><upload>
Title: Link uploaded file to an entry field
ThreadText: # Link uploaded file to an entry field
  
  (Asked by: username_0 on 2021-10-26)
  
  I want a user to upload a PDF file to his request. On the backend, I can manage to save the file on server, but I'm looking for a way to save it in the Entry just as if I was doing it manually on the admin panel but don't know how.
  So far I tried different things but I'm not sure what goes in the "cadFile" field as a handle to the actual file. Any help would be appreciated.
  
  ```
  if($_FILES){
    $myFile = UploadedFile::getInstanceByName('cadFile');
    $success = $myFile->saveAs('assets/order/' . $myFile->baseName . '.' . $myFile->extension);
  
    if($success) {
      $model->setFieldValue('cadFile', [$myFile->baseName . '.' . $myFile->extension]);
      $success = Craft::$app->elements->saveElement($model, false);
  
  ```
  
  **Tags:** assets, upload
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-10-27. Score: 2 | Accepted answer
  
  You need to register the file into the assets.
  We can do like this :
  
  ```
          $myFile = UploadedFile::getInstanceByName('cadFile');
          $success = $myFile->saveAs('assets/order/' . $myFile->baseName . '.' . $myFile->extension);
          $asset                          = new Asset();
          $asset->tempFilePath            = 'assets/order/' . $myFile->baseName . '.' . $myFile->extension;
          $asset->filename                = $myFile->baseName . '.' . $myFile->extension;
          $asset->volumeId                = 1;
          $asset->setScenario(Asset::SCENARIO_CREATE);
          $result = Craft::$app->getElements()->saveElement($asset);
  
  ```
  
  So that code will upload your code and register it as asset, for the volumeId you can replace it with your volume id.
  Then after that we need to save the assets in your entry. To save it on your field you need to pass your asset id inside array.
  
  ```
   entry->setFieldValue('cadFile', [asset->id]);
   $success = Craft::$app->elements->saveElement($entry, false);
  
  ```
  
  
  ---
  
  