<p>craft/storage/ is used for all sorts of things, so there’s no single API for storing things in there. Mostly you would just be working with PHP’s standard IO functions like <a href="http://php.net/manual/en/function.file-put-contents.php" rel="nofollow noreferrer">file_put_contents()</a>. There are some handy functions in Craft’s IOHelper class too.</p>

<p>You can get the path to the storage folder via <a href="https://craftcms.com/classreference/services/PathService#getStoragePath-detail" rel="nofollow noreferrer">PathService::getStoragePath()</a>:</p>



<pre class="lang-php prettyprint-override"><code>$storagePath = craft()-&gt;path-&gt;getStoragePath();
</code></pre>

<p>IOHelper::ensureFolderExists() is a handy function for creating a subfolder for your plugin within craft/storage:</p>

<pre class="lang-php prettyprint-override"><code>$myStoragePath = $storagePath.'myplugin/';
IOHelper::ensureFolderExists($myStoragePath);
</code></pre>

CC BY-SA 4.0
2021-04-09T16:25:25.947Z
It did: https://craftcms.stackexchange.com/questions/1952/is-there-a-way-to-generate-a-url-to-an-image-in-the-craft-storage-myplugin-direc
Ole
CC BY-SA 3.0
2014-08-15T16:36:19.630Z
Awesome, I kinda figured that may be the case, I just wanted to check before I wrote the code.
Justin Kimbrell
CC BY-SA 3.0
2014-08-15T16:48:19.367Z
One other quick question, is there was way to serve the images that I am saving in storage on the front-end? Is there way to generate a url to that? I am thinking there isn't a way since it's above web root. Anyway you can think of to get around that?
Justin Kimbrell
CC BY-SA 3.0
2014-08-15T16:49:10.617Z
@JustinKimbrell Probably warrants a new question :)
Brandon Kelly
CC BY-SA 3.0
2014-08-15T16:10:29.483Z
2018-02-21T21:27:17.960Z
2018-02-21T21:27:17.960Z
Brandon Kelly
<p>I am building a plugin that needs to cache generated images into a directory in Craft, presumably the <code>craft/storage</code> directory. I took a glance in the docs, but couldn't find much in the form of how to use storage. When building similar functionality in EE I used the their <code>cache</code> directory, so looking for something similar with Craft.</p>

<p>So how does one cache generated files in local directory so they can be used later to prevent the overhead of constantly having to generate the image?</p>

<plugin-development><custom-field>
How does a plugin make use of Craft's storage?
# How does a plugin make use of Craft's storage?

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

I am building a plugin that needs to cache generated images into a directory in Craft, presumably the `craft/storage` directory. I took a glance in the docs, but couldn't find much in the form of how to use storage. When building similar functionality in EE I used the their `cache` directory, so looking for something similar with Craft.

So how does one cache generated files in local directory so they can be used later to prevent the overhead of constantly having to generate the image?

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


---

## 1 answers

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

craft/storage/ is used for all sorts of things, so there’s no single API for storing things in there. Mostly you would just be working with PHP’s standard IO functions like file_put_contents(). There are some handy functions in Craft’s IOHelper class too.
You can get the path to the storage folder via PathService::getStoragePath():
$storagePath = craft()->path->getStoragePath();

IOHelper::ensureFolderExists() is a handy function for creating a subfolder for your plugin within craft/storage:
$myStoragePath = $storagePath.'myplugin/';
IOHelper::ensureFolderExists($myStoragePath);

> **Comments:**
> **username_0**: Awesome, I kinda figured that may be the case, I just wanted to check before I wrote the code.  
> **username_0**: One other quick question, is there was way to serve the images that I am saving in storage on the front-end? Is there way to generate a url to that? I am thinking there isn't a way since it's above web root. Anyway you can think of to get around that?  
> **username_1**: @JustinKimbrell Probably warrants a new question :)  
> **username_2**: It did: https://craftcms.stackexchange.com/questions/1952/is-there-a-way-to-generate-a-url-to-an-image-in-the-craft-storage-myplugin-direc  

---

