Access images from a media folder in the frontend
11
Method1: Use the storage_path
helper and the File::files
method to list the images in a path.
==
function onInit()
{
$dir = storage_path('app/media/your-folder');
$this['files'] = File::files($dir);
}
==
<ul>
{% for file in files %}
<li>{{ file }}</li>
{% endfor %}
</ul>
Method2: Use the Storage::files
method to list the images in a path.
==
function onInit()
{
$dir = 'media/your-folder';
$this['files'] = Storage::files($dir);
}
==
<ul>
{% for file in files %}
<li>{{ file }}</li>
{% endfor %}
</ul>
Note: Method1 returns an array of Symfony\Component\Finder\SplFileInfo
objects which give much more information about the files (size, perms, owner, etc).
There is also a covenient method directly in the MediaLibrary: https://github.com/octobercms/october/blob/develop/modules/system/classes/MediaLibrary.php#L114
This does not seem to work for me. I get an error for both methods:
Parse error: syntax error, unexpected '$dir' (T_VARIABLE), expecting ';' or '{'
Really like October CMS and would love some help?
Nevermind, just me being stupid was missing a "{" after the onInit
function onInit() { $dir = 'media/stories/aisling/'; $this['files'] = Storage::files($dir); }