Page 1 of 1
How to fetch all uploaded images
Posted: Thu Mar 03, 2022 1:36 pm
by koljaxyz
Hello there,
in the backend, i uploaded a bunch of images. Now i want to render these in my template inside a foreach loop. I do not want to place the images inside the text editor, because i want text and images separately. I know i can get the cover image with
But is there a way to get all the images? Something like:
Code: Select all
<?php foreach ($images as $image): ?>
<img src="<?= echo $image->url() ?>">
<?php endforeach ?>
Re: How to fetch all uploaded images
Posted: Thu Mar 03, 2022 10:32 pm
by Edi
It's not so easy because the images are not stored in a database file ($page->coverImage() gets the information from the database file pages.php).
Therefore a script first has to add the names of the files of the images in the directory with the images of a page to an array. Then the function foreach can be used.
Re: How to fetch all uploaded images
Posted: Thu Mar 03, 2022 11:26 pm
by novafacile
Maybe the ImageGallery plugin could be a solution for you. There you can manage images in albums and add the albums in the end of the page, without adding them in the text editor.
Re: How to fetch all uploaded images
Posted: Fri Mar 04, 2022 5:49 am
by nogajun
I had the same problem.
I figured out that I needed to get a file list, so I wrote some simple code.
Code: Select all
<?php
// set upload path
$upload_path = PATH_UPLOADS_PAGES.$page->uuid()."/";
// fetch image list
$image_list = Filesystem::listFiles($upload_path, '*', '*');
// output img tag
foreach($image_list as $l):
echo "<img src=\"".HTML_PATH_UPLOADS_PAGES.$page->uuid()."/".basename($l)."\">\n";
endforeach;
?>
Re: How to fetch all uploaded images
Posted: Fri Mar 04, 2022 9:18 am
by koljaxyz
nogajun wrote: ↑Fri Mar 04, 2022 5:49 am
I had the same problem.
I figured out that I needed to get a file list, so I wrote some simple code.
Nice! This worked, thank you! ♥