<p>I’ve had the problem with <code>storeFolder()</code> that it doesn’t create a folder that is accessible via the control panel or the file system.</p>

<p>I find <a href="https://craftcms.com/classreference/services/AssetsService#createFolder-detail" rel="nofollow noreferrer">createFolder()</a> much easier to use and it did what I expected:</p>

<pre><code>$response = craft()-&gt;assets-&gt;createFolder($parentFolderId, $folderName);
$folderId = $response-&gt;getResponseData()['folderId'];
</code></pre>

CC BY-SA 3.0
2016-11-28T20:34:54.400Z
This is the right way of doing things. This not only also calls the storeFolder method, but also checks for naming conflicts.
Andris Sevcenko
CC BY-SA 3.0
2016-11-28T14:55:45.187Z
2016-11-28T14:55:45.187Z
medoingthings
<p>To answer my own question, I found the answer in the source code.</p>

<pre><code>$newFolder = new AssetFolderModel(
                array(
                    'parentId' =&gt; $currentFolderid,
                    'name' =&gt; $folderName,
                    'sourceId' =&gt; $currentFolderid,
                    'path' =&gt; trim($folderName, '/').'/'
                )
            );
            $folderId = craft()-&gt;assets-&gt;storeFolder($newFolder);
</code></pre>

<p>Basically you can specify a <code>parentId</code>, witch is the ID of the parent folder.</p>

CC BY-SA 3.0
2016-11-28T20:32:40.200Z
This is not the correct answer. Calling storeFolder just creates a folder in the database, not on the actual Asset source. @medoingthings has posted a much better way.
Andris Sevcenko
CC BY-SA 3.0
2015-12-21T12:58:04.237Z
2015-12-21T12:58:04.237Z
ladrua
<p>I have a plugin that uploads and associates files/assets to users and entries. I would like it to store these files under a user ID subfolder of the asset folder. I know how to do this within the controlpanel, working with assets there. But how would I create and register subfolders within my plugin? If I just create the folder with my php script, it doesnt show up in the assets folder until i do a "Update Asset Indexes".</p>

<p>The way my plugin works now, It creates an asset with the craft()->assets->insertFileByLocalPath, and then associates the fileId with my entry after its created/uploaded. Maybe this is not the right way to do it?</p>

<p>Any help much appreciated.</p>

<plugin-development><assets><subfolder>
How to create a new subfolder to an asset with plugin
# How to create a new subfolder to an asset with plugin

(Asked by: username_0 on 2015-11-03)

I have a plugin that uploads and associates files/assets to users and entries. I would like it to store these files under a user ID subfolder of the asset folder. I know how to do this within the controlpanel, working with assets there. But how would I create and register subfolders within my plugin? If I just create the folder with my php script, it doesnt show up in the assets folder until i do a "Update Asset Indexes".

The way my plugin works now, It creates an asset with the craft()->assets->insertFileByLocalPath, and then associates the fileId with my entry after its created/uploaded. Maybe this is not the right way to do it?

Any help much appreciated.

**Tags:** plugin-development, assets, subfolder

---

## 2 answers

### Answer by username_1 on 2016-11-28. Score: 3 | Accepted answer

I’ve had the problem with storeFolder() that it doesn’t create a folder that is accessible via the control panel or the file system.
I find createFolder() much easier to use and it did what I expected:
$response = craft()->assets->createFolder($parentFolderId, $folderName);
$folderId = $response->getResponseData()['folderId'];

> **Comments:**
> **username_2**: This is the right way of doing things. This not only also calls the storeFolder method, but also checks for naming conflicts.  

---

### Answer by username_0 on 2015-12-21. Score: 1

To answer my own question, I found the answer in the source code.
$newFolder = new AssetFolderModel(
                array(
                    'parentId' => $currentFolderid,
                    'name' => $folderName,
                    'sourceId' => $currentFolderid,
                    'path' => trim($folderName, '/').'/'
                )
            );
            $folderId = craft()->assets->storeFolder($newFolder);

Basically you can specify a parentId, witch is the ID of the parent folder.

> **Comments:**
> **username_2**: This is not the correct answer. Calling storeFolder just creates a folder in the database, not on the actual Asset source. @medoingthings has posted a much better way.  

---

