<pre><code>if ($upload-&gt;isSuccess()) {
     $fileId = $upload-&gt;getDataItem('fileId');
     $asset = craft()-&gt;assets-&gt;getFileById($fileId);
     $folderId = $asset-&gt;getFolder()-&gt;getAttribute('id');
     ...
 }
</code></pre>

CC BY-SA 3.0
2017-10-18T11:16:16.383Z
2017-10-18T11:16:16.383Z
Fuglsetrampen
<p>I am able to upload images to an S3 bucket successfully, but I am really stumped as to how it's working.</p>

<p>I have a <code>file</code> input in my template, and in my controller I am doing this:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = craft()-&gt;config-&gt;get('section', 'mySection');
$criteria-&gt;limit = 1;
$criteria-&gt;status = null;
$criteria-&gt;authorId = craft()-&gt;request-&gt;getPost('userId');
// have also tried getting the user like this for sanity:
// get the same id either way
//$user = craft()-&gt;userSession-&gt;getUser();
//$criteria-&gt;authorId = $user-&gt;id; 

$entry = $criteria-&gt;first();

if ($entry) {
    ...
    $folder = craft()-&gt;assets-&gt;findFolder(array(
        'sourceId' =&gt; $this-&gt;assetSourceId,
    ));
    error_log($folder-&gt;id) // 207

    $upload = craft()-&gt;assets-&gt;insertFileByLocalPath(
        $tempFilePath,
        $photo-&gt;getName(),
        $folder-&gt;id,
        AssetConflictResolution::KeepBoth
     );
     if ($upload-&gt;isSuccess()) {
         ...
     }
}
</code></pre>

<p>Everything works great. The image is uploaded to the correct S3 bucket.
But when doing the same in the CP the folder ID is actually different. It looks like it should be 27?</p>

<p><a href="https://i.sstatic.net/vKzXc.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vKzXc.png" alt="enter image description here"></a></p>

<p>I'm also trying to upload another file <code>my-file.json</code> from the same front-end form. I am using the same technique above to get the folder ID, and it is returning the same number (207). However the asset is getting uploaded to the <em>correct</em> folder (207). But in my case that is actually the <em>incorrect</em> folder. It should be uploading to 27.</p>

<p>Is there a more effective way to get the correct folder Id? Thank you for any suggestions! </p>

<p>FWIW, folder 207 is the first folder in my asset list...</p>

<p><strong>UPDATE</strong></p>

<p>I'm just going to keep updating this as I fumble through it and revise it to an answer when I figure it out.</p>

<p>Using <code>craft()-&gt;assets-&gt;findFolder()</code> will <a href="https://craftcms.com/classreference/services/AssetsService#findFolder-detail" rel="nofollow noreferrer">find the first folder that matches a given criteria.</a></p>

<p>Since in my above example, I am only passing in the source Id, that means that I'm only going to get back the first folder for that resource. (In my case that would be folder 207).</p>

<p>By changing the <code>findFolder()</code> params to this:</p>

<pre><code>$user = craft()-&gt;userSession-&gt;getUser();
...
$folder = craft()-&gt;assets-&gt;findFolder(array(
    'sourceId' =&gt; $this-&gt;assetSourceId,
    'name' =&gt; $user-&gt;username
));
</code></pre>

<p>I am now getting the correct folder Id back. How I was able to upload images successfully prior to this is still a mystery.</p>

<plugin-development><assets>
How can I get the correct folder ID when uploading assets from a front-end form/plugin?
# How can I get the correct folder ID when uploading assets from a front-end form/plugin?

(Asked by: username_0 on 2017-03-13)

I am able to upload images to an S3 bucket successfully, but I am really stumped as to how it's working.
I have a file input in my template, and in my controller I am doing this:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = craft()->config->get('section', 'mySection');
$criteria->limit = 1;
$criteria->status = null;
$criteria->authorId = craft()->request->getPost('userId');
// have also tried getting the user like this for sanity:
// get the same id either way
//$user = craft()->userSession->getUser();
//$criteria->authorId = $user->id; 

$entry = $criteria->first();

if ($entry) {
    ...
    $folder = craft()->assets->findFolder(array(
        'sourceId' => $this->assetSourceId,
    ));
    error_log($folder->id) // 207

    $upload = craft()->assets->insertFileByLocalPath(
        $tempFilePath,
        $photo->getName(),
        $folder->id,
        AssetConflictResolution::KeepBoth
     );
     if ($upload->isSuccess()) {
         ...
     }
}

Everything works great. The image is uploaded to the correct S3 bucket.
But when doing the same in the CP the folder ID is actually different. It looks like it should be 27?

I'm also trying to upload another file my-file.json from the same front-end form. I am using the same technique above to get the folder ID, and it is returning the same number (207). However the asset is getting uploaded to the correct folder (207). But in my case that is actually the incorrect folder. It should be uploading to 27.
Is there a more effective way to get the correct folder Id? Thank you for any suggestions! 
FWIW, folder 207 is the first folder in my asset list...
UPDATE
I'm just going to keep updating this as I fumble through it and revise it to an answer when I figure it out.
Using craft()->assets->findFolder() will find the first folder that matches a given criteria.
Since in my above example, I am only passing in the source Id, that means that I'm only going to get back the first folder for that resource. (In my case that would be folder 207).
By changing the findFolder() params to this:
$user = craft()->userSession->getUser();
...
$folder = craft()->assets->findFolder(array(
    'sourceId' => $this->assetSourceId,
    'name' => $user->username
));

I am now getting the correct folder Id back. How I was able to upload images successfully prior to this is still a mystery.

**Tags:** plugin-development, assets

---

## 1 answers

### Answer by username_1 on 2017-10-18. Score: 1

if ($upload->isSuccess()) {
     $fileId = $upload->getDataItem('fileId');
     $asset = craft()->assets->getFileById($fileId);
     $folderId = $asset->getFolder()->getAttribute('id');
     ...
 }


---

