Answers.Body: <p>You can see an entire example how to fetch a file via curl, save it into an asset and relate it to an entry <a href="https://github.com/Anubarak/api-caller/blob/master/src/services/ApiCallerService.php" rel="nofollow noreferrer">here</a></p>
  
  <pre class="lang-PHP prettyprint-override"><code>// get the entries service
  $entries = Craft::$app-&gt;getElements();
  
  // get the entry
  $entry = \craft\elements\Entry::find()-&gt;one();
  
  // somehow grab the path of your image
  $client = Craft::createGuzzleClient();
  $response = $client-&gt;get('https://www.google.com/logos/2018/halloween18/rc5/cta.png');
  
  $image = $response-&gt;getBody()-&gt;getContents();
  $path = Craft::$app-&gt;getPath()-&gt;getTempPath() . DIRECTORY_SEPARATOR  . 'someFile.png';
  FileHelper::writeToFile($path, $image);
  
  // the folder for the image
  $folder = Craft::$app-&gt;getAssets()-&gt;getFolderById(1);
  
  // create a new asset
  $asset = new \craft\elements\Asset();
  $asset-&gt;tempFilePath = $path;
  $asset-&gt;filename = 'someFileName.jpg';
  $asset-&gt;title = 'someTitle';
  $asset-&gt;newFolderId = $folder-&gt;id;
  $asset-&gt;volumeId = $folder-&gt;volumeId;
  $asset-&gt;avoidFilenameConflicts = true;
  $asset-&gt;setScenario(\craft\elements\Asset::SCENARIO_CREATE);
  if($entries-&gt;saveElement($asset) === true){
      // file was saved 
      $entry-&gt;setFieldValue('assetFieldHandle', [$asset-&gt;id]);
      $entries-&gt;saveElement($entry);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-31T15:10:58.753Z
Answers.LastActivityDate: 2018-10-31T15:19:37.093Z
Answers.LastEditDate: 2018-10-31T15:19:37.093Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I need to be able to side load an image and attach to an entry.</p>
  
  <p>My image is coming from an external url... such as <a href="http://someplace.com/image.jpg" rel="nofollow noreferrer">http://someplace.com/image.jpg</a> - i can store this in a temporary file so its locally accessible.  But then how do I upload this image (without using $_POST or $_FILES) and attach to an entry.</p>
  
  <p>The method i'm trying at the moment i'm using something like:
  uploadNewAsset(UploadedFile $uploadedFile, $folderId)</p>
  
  <p>But UploadedFile seems to only want to accept POST variables, unless i'm missing something</p>
  
  <p>Any help would be great</p>
  
  <p>Thanks</p>
  
Tags: <plugin-development><assets><image>
Title: Sideloading an image from a function in a plugin
ThreadText: # Sideloading an image from a function in a plugin
  
  (Asked by: username_0 on 2018-10-31)
  
  I need to be able to side load an image and attach to an entry.
  
  My image is coming from an external url... such as <http://someplace.com/image.jpg> - i can store this in a temporary file so its locally accessible. But then how do I upload this image (without using $\_POST or $\_FILES) and attach to an entry.
  
  The method i'm trying at the moment i'm using something like:
  uploadNewAsset(UploadedFile $uploadedFile, $folderId)
  
  But UploadedFile seems to only want to accept POST variables, unless i'm missing something
  
  Any help would be great
  
  Thanks
  
  **Tags:** plugin-development, assets, image
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-31. Score: 1
  
  You can see an entire example how to fetch a file via curl, save it into an asset and relate it to an entry [here](https://github.com/Anubarak/api-caller/blob/master/src/services/ApiCallerService.php)
  
  ```PHP
  // get the entries service
  $entries = Craft::$app->getElements();
  
  // get the entry
  $entry = \craft\elements\Entry::find()->one();
  
  // somehow grab the path of your image
  $client = Craft::createGuzzleClient();
  $response = $client->get('https://www.google.com/logos/2018/halloween18/rc5/cta.png');
  
  $image = $response->getBody()->getContents();
  $path = Craft::$app->getPath()->getTempPath() . DIRECTORY_SEPARATOR  . 'someFile.png';
  FileHelper::writeToFile($path, $image);
  
  // the folder for the image
  $folder = Craft::$app->getAssets()->getFolderById(1);
  
  // create a new asset
  $asset = new \craft\elements\Asset();
  $asset->tempFilePath = $path;
  $asset->filename = 'someFileName.jpg';
  $asset->title = 'someTitle';
  $asset->newFolderId = $folder->id;
  $asset->volumeId = $folder->volumeId;
  $asset->avoidFilenameConflicts = true;
  $asset->setScenario(\craft\elements\Asset::SCENARIO_CREATE);
  if($entries->saveElement($asset) === true){
      // file was saved 
      $entry->setFieldValue('assetFieldHandle', [$asset->id]);
      $entries->saveElement($entry);
  }
  
  ```
  
  
  ---
  
  