<p>You can query for assets by using the <code>craft.assets()</code> query factory, see the documentation on <a href="https://craftcms.com/docs/3.x/assets.html#querying-assets" rel="nofollow noreferrer">Querying Assets</a> for details.</p>
<p>There are a couple of ways to narrow down a query. If the folder you mentioned is it's own volume, you can <a href="https://craftcms.com/docs/3.x/assets.html#volume" rel="nofollow noreferrer">query by volume</a> to get all files in that volume. To narrow it down to a single folder in the volume, you need to know the <a href="https://craftcms.com/docs/3.x/assets.html#folderid" rel="nofollow noreferrer">folder ID</a>. You can get the folder ID using the <a href="https://docs.craftcms.com/api/v3/craft-services-assets.html" rel="nofollow noreferrer">assets service</a>.</p>
<pre class="lang-php prettyprint-override"><code>&lt;?php

use craft\elements\Asset;

$volumeService = Craft::$app-&gt;getVolumes();
$assetService = Craft::$app-&gt;getAssets();

// replace with the handle of the volume your folder is in
$imageVolume = $volumeService-&gt;getVolumeByHandle('images');

// replace with the path of the folder you want to query for assets in, relative to the volume root
$imageStorageDir = 'user/uploads';

$imageFolderId = $assetService-&gt;ensureFolderByFullPathAndVolume($imageStorageDir, $imageVolume);

$assetsInFolder = Asset::find()-&gt;folderId($imageFolderId)-&gt;includeSubfolders()-&gt;all();
</code></pre>
<p>Leave out <code>includeSubfolders</code> if you don't want to include subfolders, obviously.</p>
<p>Here's the same thing slightly condensed in Twig:</p>
<pre class="lang-twig prettyprint-override"><code>{% set imageVolume = craft.app.getVolumes().getVolumeByHandle('images') %}
{% set imageStorageDir = 'user/uploads' %}
{% set imageFolderId = craft.app.getAssets().ensureFolderByFullPathAndVolume(imageStorageDir, imageVolume) %}

{% set assetsInFolder = craft.assets()
    .folderId(imageFolderId)
    .includeSubfolders()
    .all()
%}
</code></pre>
<p>Keep in mind that in order to be found by asset queries, each asset must exist in the database. This is the case if you're uploading assets through the backend or adding them programmatically – but not if you're just uploading a media folder to the server.</p>

CC BY-SA 4.0
2021-11-08T14:38:57.663Z
2022-02-01T08:58:58.050Z
2022-02-01T08:58:58.050Z
MoritzLost
<p>I'm new in Craft CMS and i want to know if it's posssible to loop every file which is in a subfolder in the Backend media folder? For Example: Show me all files in Backend/craft/web/media/images/database.</p>
<p>for example</p>
<pre><code>    {% for files in &quot;&quot;directory to folder&quot;&quot;.database.all() %}
    &lt;div class=&quot;database&quot;&gt;
        &lt;div class=&quot;database__wrapper&quot;&gt;
            &lt;div class=&quot;databsae__file&quot;&gt;
                {% set file = file.databaseImage.one() %}
                &lt;img class=&quot;database__image&quot; src=&quot;{{ file.url() }}&quot; alt=&quot;{{file.title }}&quot;&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    {% endfor %}
</code></pre>
<p>Thx</p>

CC BY-SA 4.0
2021-11-15T18:57:23.883Z
are the files stored in a craft asset volume? I believe that would be the best way to loop though files in a directory. You can review how to query the assets on this page: https://craftcms.com/docs/3.x/assets.html#example
Clark Nelson
<assets>
How to show all files from a subfolder in craft Backend
# How to show all files from a subfolder in craft Backend

(Asked by: username_0 on 2021-11-08)

I'm new in Craft CMS and i want to know if it's posssible to loop every file which is in a subfolder in the Backend media folder? For Example: Show me all files in Backend/craft/web/media/images/database.
for example
    {% for files in ""directory to folder"".database.all() %}
    
        
            
                {% set file = file.databaseImage.one() %}
                
            
        
    
    {% endfor %}

Thx

**Tags:** assets
> **Comments:**
> **username_1**: are the files stored in a craft asset volume? I believe that would be the best way to loop though files in a directory. You can review how to query the assets on this page: https://craftcms.com/docs/3.x/assets.html#example  

---

## 1 answers

### Answer by username_2 on 2021-11-08. Score: 1

You can query for assets by using the craft.assets() query factory, see the documentation on Querying Assets for details.
There are a couple of ways to narrow down a query. If the folder you mentioned is it's own volume, you can query by volume to get all files in that volume. To narrow it down to a single folder in the volume, you need to know the folder ID. You can get the folder ID using the assets service.
getVolumes();
$assetService = Craft::$app->getAssets();

// replace with the handle of the volume your folder is in
$imageVolume = $volumeService->getVolumeByHandle('images');

// replace with the path of the folder you want to query for assets in, relative to the volume root
$imageStorageDir = 'user/uploads';

$imageFolderId = $assetService->ensureFolderByFullPathAndVolume($imageStorageDir, $imageVolume);

$assetsInFolder = Asset::find()->folderId($imageFolderId)->includeSubfolders()->all();

Leave out includeSubfolders if you don't want to include subfolders, obviously.
Here's the same thing slightly condensed in Twig:
{% set imageVolume = craft.app.getVolumes().getVolumeByHandle('images') %}
{% set imageStorageDir = 'user/uploads' %}
{% set imageFolderId = craft.app.getAssets().ensureFolderByFullPathAndVolume(imageStorageDir, imageVolume) %}

{% set assetsInFolder = craft.assets()
    .folderId(imageFolderId)
    .includeSubfolders()
    .all()
%}

Keep in mind that in order to be found by asset queries, each asset must exist in the database. This is the case if you're uploading assets through the backend or adding them programmatically – but not if you're just uploading a media folder to the server.


---

