# Solution to protect files

**URL:** <https://forum.getkirby.com/t/solution-to-protect-files/29646>\
**Category:** Questions\
**Tags:** v3\
**Created:** [October 3, 2023, 7:47am UTC](https://forum.getkirby.com/t/solution-to-protect-files/29646 "2023-10-03T07:47:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![denkstream](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/denkstream/32/6628_2.png) [@denkstream](https://forum.getkirby.com/u/denkstream)\
**Post date:** [October 3, 2023, 7:47am UTC](https://forum.getkirby.com/t/solution-to-protect-files/29646/1 "2023-10-03T07:47:10Z")

</div>

Hey everyone,

I’ve implemented this [solution to protect pages](https://getkirby.com/docs/cookbook/security/access-restriction), it works very well. But the files are still accessible if you have a URL.

What would be the best way to implement file protection so that only logged in users can access them?

This is how the pages where the files are stored look like (simplified structure):

```auto
- home
- datenbank
-- urlaubsantrag
--- document.txt
--- urlaubsantrag.docx
-- zwischenbericht
--- document.txt
--- zwischenbericht.docx
-- abschlussbericht
--- document.txt
--- abschlussberich.docx

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [October 3, 2023, 8:29am UTC](https://forum.getkirby.com/t/solution-to-protect-files/29646/2 "2023-10-03T08:29:34Z")

</div>

See [Protecting files behind a firewall | Kirby CMS](https://getkirby.com/docs/cookbook/security/files-firewall)

---

<div class="post-metadata">

**Author:** ![denkstream](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/denkstream/32/6628_2.png) [@denkstream](https://forum.getkirby.com/u/denkstream)\
**Post date:** [October 3, 2023, 8:48am UTC](https://forum.getkirby.com/t/solution-to-protect-files/29646/3 "2023-10-03T08:48:14Z")

</div>

This is the first thing I tried, but then I noticed that there are no templates assigned to files. For example, in the cookbook, files with template `protected` are filtered.

I have about 900+ files that are grandfathered from Kirby 2 and therefore have no file template attached to them. ☹

The strucutre is one subpage with page template document, and file linked in field, this is how document.txt file looks like for this folder:

```auto
...
-- urlaubsantrag
--- document.txt
--- urlaubsantrag.docx

```

```auto
// document.txt content

Title: Urlaubsantrag
----
Documentselect: urlaubsantrag.docx

```

---

<div class="post-metadata">

**Author:** ![denkstream](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/denkstream/32/6628_2.png) [@denkstream](https://forum.getkirby.com/u/denkstream)\
**Post date:** [October 3, 2023, 11:41am UTC](https://forum.getkirby.com/t/solution-to-protect-files/29646/4 "2023-10-03T11:41:04Z")

</div>

I read this topic:

> [@How to block access to files](https://forum.getkirby.com/t/how-to-block-access-to-files/24953/4):
>
> The problem here is that you want to apply the protected files to all pages, not a specific page like in the recipe. Therefore, you need something in your file route, that let’s you differentiate between a page and a file. The easiest way to achieve this, is to use a string between the page id and the filename in the file url: Kirby::plugin('cookbook/files-firewall', [ 'components' =\> [ 'file::url' =\> function ($kirby, $file) { return $kirby-\>url() . '/' . $file-\>parent()-\>id() . …

and this helped definitely 🙂
