Global Images for all pages

Is it possible to store images in a folder and access them from all pages?

Thx

Yes, you can use the site settings to store global images and access them (for example) like this …

<?php $images = $site->images() ?>

More information about the site object:
https://getkirby.com/docs/cheatsheet#site

ok, pretty cool… but how can i select the files in the panel? I´m using the gallery field and want to access pagefiles and sitefiles… is that possible too?

I think the gallery field works via drag and drop from the sidebar, so that won’t work with images stored in the site folder.

What you can do, is use a structure field with a select field, and fill the select options with images from the site.

1 Like

@texnixe Just had a mad idea. PHP can create symlinks. Ive not tried it but in theory you could symlink a global folder of images into each page template. Would that work with the panel?

I don’t think that this will work, because Kirby doesn’t store the files related to a page in a subfolder which could be symlinked.

But maybe I don’t get your idea right?! :thinking:

@jimbobrjames Hm, interesting idea. But wouldn’t you have to link to each individual file in the media folder? If you just link to the folder, it will appear as a subpage but you still wouldn’t see any images. But if you create a symlink to each file in the media folder, a new symlink to each page would have to be created if a new file is uploaded.

Plus, you’d have to use another hook to create a symlink to the media folder every time a new page is created.

Another issue: if you delete the original file, your symlink will still exist, unless you remove it via yet another hook.

Also, I don’t know if there are any performance issues related to having hundreds or thousands of symlinks and lots of images in the media folder.

So yes, in general symlinks do work, but you would need a lot of keeping track of changes in the background.

Will you? Call me silly but wont this let you set it and forget it?

<?php symlink(echo $site->diruri(), echo $page->diruri()); ?>

Where do you want to put that piece of code?

In a template or a snippet.

How does that help you in the Panel? If you upload a new file, you would have to call that code; but as I said, it does not help to link the folder, anyway, you need to link the files. Have you tried it yet?

No i just got out of bed… i will make some tea and then try when i have woken up. :slight_smile:

Remove the echo commands first, after your tea but before you try :wink:

I thought that didn’t look quite right. I think the theory is sound though. I sometimes use Textpattern and i have global plugin cache that is symlinked into the each site to save downloading and installing multiple times. It works and i don’t have to symlink each file individually. I can call the files as if they were in that folder.

I am armed with tea… here i go…

Thing is, if you try to link one directory to an existing one using symlink() you will get a file exists error. What you can do, is loop through all the files and link them to a filename in the new directory (but only do this once, otherwise you will get the same error).

I’m pretty sure you have to do this via hooks for each file you upload and each page your create individually.

Well, perhaps but thats beyond me. Im only just starting out with PHP.

Yup. Your right. file exists warning… i went with this…

    <?php
    $srcpathdir = array(getcwd(),'/content/','gimages/');
    $destpathdir = array(getcwd(),'/content/',$page->diruri(),'/');

    $srcpath = join("",$srcpathdir);
    $destpath = join("",$destpathdir);

    symlink($srcpath, $destpath);

    ?>

What on earth? (and some more chars)

page('gimages')->root();
$page->root();

Yeah ok it could be smaller. But for that i need breakfast. Did i mention i’ve been learning PHP for like a week?

It’s all in the Cheat Sheet. And I think you’ve been doing Kirby stuff for more than a week, haven’t you?

But ok, you are very creative in finding your way around, so that’s perfectly fine :slightly_smiling_face: