Answers.Body: <p>I think your error is something like &quot;newlocation cannot be empty&quot;. I solved this in the following way:</p>
  <pre><code>$asset = new Asset();
  $asset-&gt;tempFilePath = $tempPath;
  $asset-&gt;filename = $fileName;
  $asset-&gt;folderId = null;
  $asset-&gt;newFolderId = $folder-&gt;id;
  $asset-&gt;kind = &quot;Image&quot;;
  $asset-&gt;title = $assetTitle;
  $asset-&gt;avoidFilenameConflicts = true;
  $asset-&gt;setVolumeId($folder-&gt;volumeId);
  $asset-&gt;setScenario(Asset::SCENARIO_CREATE);
  
  $asset-&gt;validate();
  Craft::$app-&gt;getElements()-&gt;saveElement($asset, false);
  </code></pre>
  <p>Hope this will get the work done.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-07T08:29:58.377Z
Answers.LastActivityDate: 2022-04-07T08:29:58.377Z
Answers.UserDisplayName: Robin
Body: <p>I'm currently making a module that I'm hoping is going to create entries and assets from an API. I've got the entries part working and now I'm trying to create an asset from a URI of an image.</p>
  <p>I've tried to use this method <a href="https://craftcms.stackexchange.com/questions/20965/how-do-i-upload-an-asset-in-craft-3-via-php">How do I upload an asset in Craft 3, via PHP</a> but I can't get it to work I'm just wondering what I'm doing wrong? Do I need to be doing something with the temp file maybe? I've put this within the same function that I'm using to create the entries.</p>
  <p>Eventually I need to loop through an array of objects where the images are stored and create an asset from each of the images but for the moment I'm just trying to get it to create one asset from a single URI of an image</p>
  <pre><code>    $uploadedFile = &quot;https:\/\/images.fakeuritoprotectsecurity.com\/images\/1\/20\/33\/0123456_789_1_XLARGE.jpg&quot;;
      $folderId = 1;
  
      if (empty($folderId)) {
          throw new BadRequestHttpException('No target destination provided for uploading');
      }
  
      if ($uploadedFile === null) {
          throw new BadRequestHttpException('No file was uploaded');
      }
  
      $assets = Craft::$app-&gt;getAssets();
  
     if ($uploadedFile-&gt;getHasError()) {
         throw new UploadFailedException($uploadedFile-&gt;error);
     }
  
      // Move the uploaded file to the temp folder
      if (($tempPath = $uploadedFile-&gt;saveAsTempFile()) === false) {
          throw new UploadFailedException(UPLOAD_ERR_CANT_WRITE);
      }
  
     // Move the uploaded file to the temp folder
     if (empty($folderId)) {
         throw new BadRequestHttpException('The target destination provided for uploading is not valid');
     }
  
      $folder = $assets-&gt;findFolder(['id' =&gt; $folderId]);
  
      if (!$folder) {
          throw new BadRequestHttpException('The target folder provided for uploading is not valid');
      }
  
      // Check the permissions to upload in the resolved folder.
      $filename = Assets::prepareAssetName($uploadedFile-&gt;name);
  
      $asset = new Asset();
      $asset-&gt;tempFilePath = $tempPath;
      $asset-&gt;filename = $filename;
      $asset-&gt;newFolderId = $folder-&gt;id;
      $asset-&gt;volumeId = $folder-&gt;volumeId;
      $asset-&gt;avoidFilenameConflicts = true;
      $asset-&gt;setScenario(Asset::SCENARIO_CREATE);
  
      $result = Craft::$app-&gt;getElements()-&gt;saveElement($asset);
      if (!$result) {
          Craft::error('Couldn’t save the entry &quot;'.$asset-&gt;filename.'&quot;', __METHOD__);
      }
  </code></pre>
  <p>Any help is really appreciated, thanks</p>
  
Tags: <entries><assets><plugins><modules><api>
Title: Creating an asset from an API using a module
ThreadText: # Creating an asset from an API using a module
  
  (Asked by: username_0 on 2022-03-22)
  
  I'm currently making a module that I'm hoping is going to create entries and assets from an API. I've got the entries part working and now I'm trying to create an asset from a URI of an image.
  
  I've tried to use this method [How do I upload an asset in Craft 3, via PHP](https://craftcms.stackexchange.com/questions/20965/how-do-i-upload-an-asset-in-craft-3-via-php) but I can't get it to work I'm just wondering what I'm doing wrong? Do I need to be doing something with the temp file maybe? I've put this within the same function that I'm using to create the entries.
  
  Eventually I need to loop through an array of objects where the images are stored and create an asset from each of the images but for the moment I'm just trying to get it to create one asset from a single URI of an image
  
  ```
      $uploadedFile = "https:\/\/images.fakeuritoprotectsecurity.com\/images\/1\/20\/33\/0123456_789_1_XLARGE.jpg";
      $folderId = 1;
  
      if (empty($folderId)) {
          throw new BadRequestHttpException('No target destination provided for uploading');
      }
  
      if ($uploadedFile === null) {
          throw new BadRequestHttpException('No file was uploaded');
      }
  
      $assets = Craft::$app->getAssets();
  
     if ($uploadedFile->getHasError()) {
         throw new UploadFailedException($uploadedFile->error);
     }
  
      // Move the uploaded file to the temp folder
      if (($tempPath = $uploadedFile->saveAsTempFile()) === false) {
          throw new UploadFailedException(UPLOAD_ERR_CANT_WRITE);
      }
  
     // Move the uploaded file to the temp folder
     if (empty($folderId)) {
         throw new BadRequestHttpException('The target destination provided for uploading is not valid');
     }
  
      $folder = $assets->findFolder(['id' => $folderId]);
  
      if (!$folder) {
          throw new BadRequestHttpException('The target folder provided for uploading is not valid');
      }
  
      // Check the permissions to upload in the resolved folder.
      $filename = Assets::prepareAssetName($uploadedFile->name);
  
      $asset = new Asset();
      $asset->tempFilePath = $tempPath;
      $asset->filename = $filename;
      $asset->newFolderId = $folder->id;
      $asset->volumeId = $folder->volumeId;
      $asset->avoidFilenameConflicts = true;
      $asset->setScenario(Asset::SCENARIO_CREATE);
  
      $result = Craft::$app->getElements()->saveElement($asset);
      if (!$result) {
          Craft::error('Couldn’t save the entry "'.$asset->filename.'"', __METHOD__);
      }
  
  ```
  
  Any help is really appreciated, thanks
  
  **Tags:** entries, assets, plugins, modules, api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-04-07. Score: 1
  
  I think your error is something like "newlocation cannot be empty". I solved this in the following way:
  
  ```
  $asset = new Asset();
  $asset->tempFilePath = $tempPath;
  $asset->filename = $fileName;
  $asset->folderId = null;
  $asset->newFolderId = $folder->id;
  $asset->kind = "Image";
  $asset->title = $assetTitle;
  $asset->avoidFilenameConflicts = true;
  $asset->setVolumeId($folder->volumeId);
  $asset->setScenario(Asset::SCENARIO_CREATE);
  
  $asset->validate();
  Craft::$app->getElements()->saveElement($asset, false);
  
  ```
  
  Hope this will get the work done.
  
  
  ---
  
  