<p><strong>Craft 2:</strong></p>
<p>Since <code>craft/storage/</code> is not publicly accessible, your only real option here is to serve them through Craft as Resource Requests. (This is the vehicle through which Craft serves up all of the CP’s front end resources, which live in <code>craft/app/resources/</code>.)</p>
<p>The actual resource URLs will depend on the site’s <a href="http://buildwithcraft.com/docs/config-settings#resourceTrigger" rel="nofollow noreferrer">resourceTrigger</a> config setting, so to generate the URLs you should use <code>URLHelper::getResourceUrl()</code>:</p>
<pre class="lang-php prettyprint-override"><code>$url = UrlHelper::getResourceUrl('myplugin/some/path.jpg');
</code></pre>
<p>Your templates can call the corresponding <code>resourceUrl()</code> function as well:</p>
<pre class="lang-twig prettyprint-override"><code>{{ resourceUrl('myplugin/some/path') }}
</code></pre>
<p>Then you will need to add a <a href="http://buildwithcraft.com/docs/plugins/hooks-reference#getResourcePath" rel="nofollow noreferrer"><code>getResourcePath</code></a> hook method to your primary plugin class, which would detect incoming resource requests and resolve them to the actual server path in <code>craft/storage/</code>.</p>
<pre class="lang-php prettyprint-override"><code>public function getResourcePath($path)
{
    // Does this path start with &quot;myplugin/&quot;?
    if (strncmp($path, 'myplugin/', 9) == 0)
    {
        // Return the path to the file in craft/storage/
        return craft()-&gt;path-&gt;getStoragePath().'myplugin/'.substr($path, 9);
    }
}
</code></pre>
<p>It’s worth mentioning that you’re always going to get worse performance out of serving files through PHP than you would if they were located somewhere that’s publicly accessible. So you might just want to have the user create a new folder somewhere under their web root, and have them specify the path/URL to that folder, just like local asset sources.</p>
<p><strong>Craft 3+:</strong></p>
<p>In Craft 3+, your plugin can define an <a href="https://craftcms.com/docs/4.x/extend/asset-bundles.html" rel="nofollow noreferrer">asset bundle</a> whose <code>sourcePath</code> points to the source folder in <code>storage/</code>.</p>

CC BY-SA 3.0
2016-11-11T22:44:23.707Z
Is there a way to find the 'public' folder? I can't find anything in PathService or constants.
Elliot Lewis
CC BY-SA 3.0
2017-02-27T19:12:47.187Z
@ElliotLewis Sorry just saw your comment. Can you please post that as a new question?
Brandon Kelly
CC BY-SA 4.0
2023-08-21T07:56:49.303Z
any updates for newer versions of craft?
tenshi
CC BY-SA 4.0
2023-08-24T15:58:09.780Z
@tenshi Just added a note for Craft 3+.
Brandon Kelly
CC BY-SA 4.0
2014-08-15T21:51:42.657Z
2023-08-24T15:57:45.177Z
2023-08-24T15:57:45.177Z
Brandon Kelly
<p>I am generating dynamic images to a directory in <code>craft/storage</code>. I need to be able to use these generated images on the front-end inside templates. Is there a helper method for this, or some way to use these images in my templates?</p>

<plugin-development><custom-fieldtype>
Is there a way to generate a url to an image in the craft/storage/myplugin directory?
# Is there a way to generate a url to an image in the craft/storage/myplugin directory?

(Asked by: username_0 on 2014-08-15)

I am generating dynamic images to a directory in `craft/storage`. I need to be able to use these generated images on the front-end inside templates. Is there a helper method for this, or some way to use these images in my templates?

**Tags:** plugin-development, custom-fieldtype

---

## 1 answers

### Answer by username_1 on 2014-08-15. Score: 4 | Accepted answer

Craft 2:
Since craft/storage/ is not publicly accessible, your only real option here is to serve them through Craft as Resource Requests. (This is the vehicle through which Craft serves up all of the CP’s front end resources, which live in craft/app/resources/.)
The actual resource URLs will depend on the site’s resourceTrigger config setting, so to generate the URLs you should use URLHelper::getResourceUrl():
$url = UrlHelper::getResourceUrl('myplugin/some/path.jpg');

Your templates can call the corresponding resourceUrl() function as well:
{{ resourceUrl('myplugin/some/path') }}

Then you will need to add a getResourcePath hook method to your primary plugin class, which would detect incoming resource requests and resolve them to the actual server path in craft/storage/.
public function getResourcePath($path)
{
    // Does this path start with "myplugin/"?
    if (strncmp($path, 'myplugin/', 9) == 0)
    {
        // Return the path to the file in craft/storage/
        return craft()->path->getStoragePath().'myplugin/'.substr($path, 9);
    }
}

It’s worth mentioning that you’re always going to get worse performance out of serving files through PHP than you would if they were located somewhere that’s publicly accessible. So you might just want to have the user create a new folder somewhere under their web root, and have them specify the path/URL to that folder, just like local asset sources.
Craft 3+:
In Craft 3+, your plugin can define an asset bundle whose sourcePath points to the source folder in storage/.

> **Comments:**
> **username_2**: Is there a way to find the 'public' folder? I can't find anything in PathService or constants.  
> **username_1**: @ElliotLewis Sorry just saw your comment. Can you please post that as a new question?  
> **username_3**: any updates for newer versions of craft?  
> **username_1**: @tenshi Just added a note for Craft 3+.  

---

