Retrieve a list of files based on a specified folder path.
To install the plugin, follow these instructions.
- Open your terminal and go to your Craft project:
cd /path/to/project
- Then tell Composer to load the plugin:
composer require youandmedigital/craft-getfiles
This little plugin retrieves a list of files based on a specified folder path.
This might be useful to you if
- You're running Craft 3.1 or above
- You need to retrieve files, not managed by Craft CMS, from a folder on your web server and return the output in Twig
Inside /assets/images there are 3 image files:
22 Apr 22:54 image01.jpg
22 Apr 22:54 image02.jpg
22 Apr 22:54 image03.gif
In our Twig templates, we set variables and give GetFiles a folder path to search:
{% set settings =
{
path: '/assets/images/'
}
%}
{% set file = craft.getfiles.config(settings) %}
<p>Available images:</p>
{% for image in images %}
<img src="{{ image }}" alt="{{ image }}">
{% endfor %}
This example Twig code would output:
<img src="/assets/images/image01.jpg" alt="image01.jpg">
<img src="/assets/images/image02.jpg" alt="image02.jpg">
<img src="/assets/images/image03.gif" alt="image03.gif">
Inside /assets/images there are 3 files:
<img src="/assets/images/image01.jpg" alt="image01.jpg">
<img src="/assets/images/image02.jpg" alt="image02.jpg">
<img src="/assets/images/image03.gif" alt="image03.gif">
In our Twig templates, we set variables, give GetFiles a folder path to search and a regex pattern to match:
{% set settings =
{
path: '/assets/images/',
pattern: '*.gif'
}
%}
{% set images = craft.getfiles.config(settings) %}
<p>Available images:</p>
{% for image in images %}
<img src="/assets/images/{{ image }}" alt="{{ image }}">
{% endfor %}
This example Twig code would output:
<p>Available images:<p>
<img src="/assets/images/image03.gif" alt="image03.gif">
- path
(string, required)
: A valid folder for GetFiles to search - pattern
(string, optional, default value '*')
: A regex pattern to match - pathFormat
(string, optional, default value '2')
: If you specify1
, the plugin will return the filename only. If you specify2
, the plugin will output the filename name relative to your base path.3
will output the absolute path to the filename.
Example configuration:
{% set myVarSettings =
{
path: '<path>',
pathformat: '<pathformat>',
pattern: '<pattern>'
}
%}
{% set myVar = craft.getfiles.config(myVarSettings) %}