Answers.Body: <p>Download the image into a temp file, then upload it as an asset. You can find $iAssetSourceId by looking at the URL when editing an asset source</p>
  
  <pre><code>$iAssetSourceId = 4;
  $iAssetFolderId = (int)craft()-&gt;assets-&gt;getRootFolderBySourceId($iAssetSourceId)-&gt;id;
  $sTempFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $sFilename;
  $sFileContents = file_get_contents($sImageUrl);
  file_put_contents($sTempFilePath, $sFileContents);
  
  // Get craft to copy the temp file into the assets folder
  $oAssetOperationResponse = craft()-&gt;assets-&gt;insertFileByLocalPath(
      $sTempFilePath,
      $sFilename,
      $iAssetFolderId,
      AssetConflictResolution::Replace
  );
  
  return (int)$oAssetOperationResponse-&gt;responseData["fileId"];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-12T12:35:24.577Z
Answers.Text: I would recommend to use the `AssetsHelper::getTempFilePath($extension);` helper for the temporary file path.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-25T10:20:14.740Z
Answers.Text: Any idea what the equivalent of `craft()->assets->insertFileByLocalPath()` is for Craft 3?
Answers.UserDisplayName: SteveEdson
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-03T19:40:59.483Z
Answers.Text: It seems Craft 3 does use `insertFileByLocalPath`. I was able to handle this by simply creating a new `Asset` element and saving it. When doing so, you just need to set the `tempFilePath` property of the element to the path you put the file to in the beginning. i.e. `$asset = new Asset(); $asset->tempFilePath = $sTempFilePath; $asset->setScenario(Asset::SCENARIO_CREATE); Craft::$app->getElements()->saveElement($asset);` You'll also want to set a few other properties such as `filename`, `folderId`, & `volumeId`.
Answers.UserDisplayName: Jonny Asmar
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-05-12T08:32:08.030Z
Answers.LastActivityDate: 2017-09-05T14:04:18.437Z
Answers.LastEditDate: 2017-09-05T14:04:18.437Z
Answers.UserDisplayName: michael
Body: <p>I've created several entries from a feed from the eBay API, but I would like to add images to the entry too! </p>
  
  <p>I've managed to get an array of URLs, which I would like to import into an assets field that I have on the new entries.</p>
  
  <p>I saw a post from Brandon from a couple years back suggesting it was not possible to do this, unable to find anything more up-to-date.</p>
  
  <p>Could I grab the image with cURL in my plugin then create a <code>new AssetFileModel</code> and some how sync the temp image file with Craft somehow? As a follow up question how would I add multiple images to the assets field on my new entry?</p>
  
  <p>Thanks
  Joe</p>
  
Tags: <plugin-development><assets><image><assetfilemodel>
Title: Is it possible to create a new asset from an image URL?
ThreadText: # Is it possible to create a new asset from an image URL?
  
  (Asked by: username_0 on 2016-05-11)
  
  I've created several entries from a feed from the eBay API, but I would like to add images to the entry too!
  
  I've managed to get an array of URLs, which I would like to import into an assets field that I have on the new entries.
  
  I saw a post from Brandon from a couple years back suggesting it was not possible to do this, unable to find anything more up-to-date.
  
  Could I grab the image with cURL in my plugin then create a `new AssetFileModel` and some how sync the temp image file with Craft somehow? As a follow up question how would I add multiple images to the assets field on my new entry?
  
  Thanks
  Joe
  
  **Tags:** plugin-development, assets, image, assetfilemodel
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-05-12. Score: 5 | Accepted answer
  
  Download the image into a temp file, then upload it as an asset. You can find $iAssetSourceId by looking at the URL when editing an asset source
  
  ```
  $iAssetSourceId = 4;
  $iAssetFolderId = (int)craft()->assets->getRootFolderBySourceId($iAssetSourceId)->id;
  $sTempFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $sFilename;
  $sFileContents = file_get_contents($sImageUrl);
  file_put_contents($sTempFilePath, $sFileContents);
  
  // Get craft to copy the temp file into the assets folder
  $oAssetOperationResponse = craft()->assets->insertFileByLocalPath(
      $sTempFilePath,
      $sFilename,
      $iAssetFolderId,
      AssetConflictResolution::Replace
  );
  
  return (int)$oAssetOperationResponse->responseData["fileId"];
  
  ```
  
  > **Comments:**
  > **username_2**: I would recommend to use the `AssetsHelper::getTempFilePath($extension);` helper for the temporary file path.  
  > **username_3**: Any idea what the equivalent of `craft()->assets->insertFileByLocalPath()` is for Craft 3?  
  > **username_4**: It seems Craft 3 does use `insertFileByLocalPath`. I was able to handle this by simply creating a new `Asset` element and saving it. When doing so, you just need to set the `tempFilePath` property of the element to the path you put the file to in the beginning. i.e. `$asset = new Asset(); $asset->tempFilePath = $sTempFilePath; $asset->setScenario(Asset::SCENARIO_CREATE); Craft::$app->getElements()->saveElement($asset);` You'll also want to set a few other properties such as `filename`, `folderId`, & `volumeId`.  
  
  ---
  
  