2

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?

6

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);
  • Awesome, I kinda figured that may be the case, I just wanted to check before I wrote the code. – Justin Kimbrell Aug 15 '14 at 16:36
  • 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 Aug 15 '14 at 16:48
  • @JustinKimbrell Probably warrants a new question :) – Brandon Kelly Aug 15 '14 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.