<p>I'm working on a plugin that creates auto crops of a selected image, but I am unsure of the proper steps to create and upload an image programatically. I am hoping that Local asset sources and Remote asset sources can be treated the same way. If not, then I would like to create that abstraction.</p>

<p>My understanding of this at the moment is that I need to both insert the file and save an asset file record. So I am first getting a local copy of the original asset that I will create crops from via <code>&lt;assetSource&gt;-&gt;getLocalCopy()</code>, and using that local path with  <code>craft()-&gt;assets-&gt;insertFileByLocalPath()</code>. </p>

<p>This works well if I use a Local asset source to store my cropped images. However, when storing my crops on Rackspace, I run into an issue where a naming conflict is not resolved. </p>

<p>I am calling <code>insertFileByLocalPath()</code> with <code>AssetConflictResolution::Replace</code>. I've noticed that <code>fileId</code> is not set in <code>BaseAssetSourceType-&gt;insertFileByPath()</code> until the response is successful, which won't happen while there is a naming conflict. Instead the response returns back to <code>craft()-&gt;assets-&gt;insertFileByLocalPath()</code>, which then tries to access <code>fileId</code> via <code>$response-&gt;getDataItem('fileId')</code> which has not been set. I then get an error stating:  </p>

<p><strong>Fatal error: Call to a member function getFolder() on null in AssetsService on line 1334</strong>. </p>

<p>The failure occurs in AssetService <code>_mergeUploadedFiles()</code> after trying to <code>getFileById()</code> where the id is null, and then calling <code>getFolder()</code> on null.</p>

<p>I am not sure if this is the best place to put this, if this a bug within Craft, or if I am going about creating and uploading Assets in the wrong way. </p>

<p>Are my steps to upload images correct thus far? Can all asset sources be treated in the same way? Any guidance or critique would be greatly appreciated.</p>

CC BY-SA 3.0
2015-11-23T14:55:51.823Z
I'm experiencing the same issue. Did anything ever come of this?
keito
CC BY-SA 3.0
2016-03-17T14:15:42.833Z
I've also had this issue myself, I'm not sure it was ever fixed.
Jay
CC BY-SA 3.0
2016-03-18T16:15:14.790Z
To update: I ended up handling the conflict and renaming myself which is annoying but works. I'm going to test in 2.5 soon and see if they have fixed this, and I'll report back with any findings.
kcolls
<plugin-development><assets><sources>
How do I upload images programmatically?
# How do I upload images programmatically?

(Asked by: username_0 on 2015-10-27)

I'm working on a plugin that creates auto crops of a selected image, but I am unsure of the proper steps to create and upload an image programatically. I am hoping that Local asset sources and Remote asset sources can be treated the same way. If not, then I would like to create that abstraction.

My understanding of this at the moment is that I need to both insert the file and save an asset file record. So I am first getting a local copy of the original asset that I will create crops from via `->getLocalCopy()`, and using that local path with `craft()->assets->insertFileByLocalPath()`.

This works well if I use a Local asset source to store my cropped images. However, when storing my crops on Rackspace, I run into an issue where a naming conflict is not resolved.

I am calling `insertFileByLocalPath()` with `AssetConflictResolution::Replace`. I've noticed that `fileId` is not set in `BaseAssetSourceType->insertFileByPath()` until the response is successful, which won't happen while there is a naming conflict. Instead the response returns back to `craft()->assets->insertFileByLocalPath()`, which then tries to access `fileId` via `$response->getDataItem('fileId')` which has not been set. I then get an error stating:

**Fatal error: Call to a member function getFolder() on null in AssetsService on line 1334**.

The failure occurs in AssetService `_mergeUploadedFiles()` after trying to `getFileById()` where the id is null, and then calling `getFolder()` on null.

I am not sure if this is the best place to put this, if this a bug within Craft, or if I am going about creating and uploading Assets in the wrong way.

Are my steps to upload images correct thus far? Can all asset sources be treated in the same way? Any guidance or critique would be greatly appreciated.

**Tags:** plugin-development, assets, sources
> **Comments:**
> **username_1**: I'm experiencing the same issue. Did anything ever come of this?  
> **username_2**: I've also had this issue myself, I'm not sure it was ever fixed.  
> **username_0**: To update: I ended up handling the conflict and renaming myself which is annoying but works. I'm going to test in 2.5 soon and see if they have fixed this, and I'll report back with any findings.  

---

## 0 answers

