<p>Here's how I did it in a recent project:</p>

<pre><code>    $fileIds = array();

    // get remote image and store in temp path
    $imageInfo = pathinfo($data['image_url']);
    $tempPath = CRAFT_STORAGE_PATH . 'runtime/temp/' . $imageInfo['basename'];
    file_put_contents($tempPath, fopen($data['image_url'], 'r'));

    // insert the file into assets
    $response = craft()-&gt;assets-&gt;insertFileByLocalPath(
      $tempPath,
      $imageInfo['basename'],
      $myAssetFolderId, // notice, this is the id of the folder you want to upload to
      AssetConflictResolution::KeepBoth
    );

    // if the response is a success, get the file id
    if ($response &amp;&amp; $response-&gt;isSuccess()) {
        $fileIds[] = $response-&gt;getDataItem('fileId');
    }

    // create a new entry
    $entry = new EntryModel();
    $entry-&gt;sectionId = $mySectionId;
    $entry-&gt;typeId = $myEntryTypeId;
    $entry-&gt;authorId = 1;

    // set your content, including image field by id
    $entry-&gt;getContent()-&gt;title = $data['title'];
    $entry-&gt;getContent()-&gt;mainImage = $fileIds;

    $success = craft()-&gt;entries-&gt;saveEntry($entry);
</code></pre>

<p>I included the entry saving part of it to show how the response is used to get the file id(s) and so on.</p>

<p>Whether this is the "correct" way to do it or not, I don't know. :)</p>

CC BY-SA 3.0
2015-09-25T22:30:02.993Z
It's correct if it works. ;)
Brad Bell
CC BY-SA 3.0
2015-10-01T07:20:21.540Z
Thanks, could you help me getting the folder id and resource id
HTDutchy
CC BY-SA 3.0
2017-06-15T11:03:09.150Z
Sorry for reviving this, but in my case, I had to change `$fileIds[] = $response->getDataItem('fileId');` to `$fileIds[] = $response->getResponseData();` because it was giving me an error when saving because the filename was empty.
Jorge Anzola
CC BY-SA 3.0
2015-09-25T22:20:23.027Z
2015-09-25T22:20:23.027Z
André Elvan
<p>I'm looking for the correct way to import an image from a url and save it to an entry and in the correct subfolder (<code>uploads/images/{id}</code>)</p>

<p>I've created a resource pointing to <code>uploads/images</code>
and the entry field will upload to <code>resource/{id}</code> by default.</p>

<p>As I understand the file also needs to be indexed in the database for it to be visible in the file browser so I can't just save it straight to the file system.</p>

<plugin-development><image>
Importing images from external url and saving to entry using plugin
# Importing images from external url and saving to entry using plugin

(Asked by: username_0 on 2015-09-24)

I'm looking for the correct way to import an image from a url and save it to an entry and in the correct subfolder (`uploads/images/{id}`)

I've created a resource pointing to `uploads/images`
and the entry field will upload to `resource/{id}` by default.

As I understand the file also needs to be indexed in the database for it to be visible in the file browser so I can't just save it straight to the file system.

**Tags:** plugin-development, image


---

## 1 answers

### Answer by username_1 on 2015-09-25. Score: 5 | Accepted answer

Here's how I did it in a recent project:
    $fileIds = array();

    // get remote image and store in temp path
    $imageInfo = pathinfo($data['image_url']);
    $tempPath = CRAFT_STORAGE_PATH . 'runtime/temp/' . $imageInfo['basename'];
    file_put_contents($tempPath, fopen($data['image_url'], 'r'));

    // insert the file into assets
    $response = craft()->assets->insertFileByLocalPath(
      $tempPath,
      $imageInfo['basename'],
      $myAssetFolderId, // notice, this is the id of the folder you want to upload to
      AssetConflictResolution::KeepBoth
    );

    // if the response is a success, get the file id
    if ($response && $response->isSuccess()) {
        $fileIds[] = $response->getDataItem('fileId');
    }

    // create a new entry
    $entry = new EntryModel();
    $entry->sectionId = $mySectionId;
    $entry->typeId = $myEntryTypeId;
    $entry->authorId = 1;

    // set your content, including image field by id
    $entry->getContent()->title = $data['title'];
    $entry->getContent()->mainImage = $fileIds;

    $success = craft()->entries->saveEntry($entry);

I included the entry saving part of it to show how the response is used to get the file id(s) and so on.
Whether this is the "correct" way to do it or not, I don't know. :)

> **Comments:**
> **username_2**: It's correct if it works. ;)  
> **username_0**: Thanks, could you help me getting the folder id and resource id  
> **username_3**: Sorry for reviving this, but in my case, I had to change `$fileIds[] = $response->getDataItem('fileId');` to `$fileIds[] = $response->getResponseData();` because it was giving me an error when saving because the filename was empty.  

---

