Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

List images from upload directory


Go to End


12 Posts   12159 Views

Avatar
Teere

Community Member, 1 Post

16 June 2010 at 3:24am

Hey!

As the topic stated, im trying to list image files from a certain folder in the upload directory (Uploaded with the built in fileuploader).
Since I wish to display these on the front page.
Being quite new to silverstripe and I cant really find any direct information on this.

Example:

image1.jpg in folder /myfrontpageimages

to

<img src="thesource/$src" alt="$alt" title="$title" />

Avatar
Hamish

Community Member, 712 Posts

17 June 2010 at 10:04am

Hi there, welcome to the community.

Files and Folders in your assets folder can be used like other database objects. You can pick a specific folder with:

$folder = DataObject::get_one("Folder", "Filename = 'assets/myfrontpageimages'");

From here it is probably easiest to get all images that have a parent ID equal to this Folders ID:

$images = DataObject::get("Image", "ParentID = '{$folder->ID}'");

The above code can be wrapped into a method in you Page class (with some additional checking that the folder exists):

function getFrontPageImages() {
  $folder = DataObject::get_one("Folder", "Filename = 'assets/myfrontpageimages'");
  return $folder ? DataObject::get("Image", "ParentID = '{$folder->ID}'") : false;
}

Now, from your template you can do:

<% if FrontPageImages %>
  <% control FrontPageImages %>
    $Tag
  <% end_control %>
<% end_if %>

(FYI $Tag calls the Image method getTag() which propduces standard <img src=".." alt=".."> HTML for your template)

Hope this helps.

Avatar
spierala

Community Member, 80 Posts

18 July 2012 at 8:33pm

Hello,
thanks that works still in SS3.

I wonder how can I make a has_one relationship between Page and Folder, so the user can set the "Images Folder" for a certain page in the backend.

Kind regards,
Florian

Avatar
lerni

Community Member, 81 Posts

19 July 2012 at 10:21am

hi florian

What have you done so far? Anyway Folder extends File. You need a has_one to a Folder and TreeDropdownField could be a Field to select this.

lukas

Avatar
spierala

Community Member, 80 Posts

21 July 2012 at 9:39am

Hey Lukas!
that worked perfectly! was quite easy that way...

My final code looked like that:

$fields->addFieldToTab('Root.Main', new TreeDropdownField('FolderID', 'Choose Image Folder', 'Folder'), 'Content');

FolderID is automatically generated for the CustomPage Table when putting this code in CustomPage.php:

static $has_one = array ( 
'Folder' => 'Folder'
);

thx,
Florian

Avatar
acktivate

Community Member, 11 Posts

14 April 2013 at 4:34am

Edited: 14/04/2013 4:57am

I am running SS3 and dropped this code onto my page.php page and inserted the the loop control on the ss page and I can't get it working. I've turned on debugging and the Filename that I am setting does not seem to get referenced.

page.php
function RotateImages() {
$folder = DataObject::get_one("Folder", "Filename = 'assets/myfrontpageimages'");
Debug::show(DataObject::get_one("Folder"));
Debug::show(DataObject::get_one("Image"));
return $folder ? DataObject::get("Image", "ParentID = '{$folder->ID}'") : false;

}
Debug Folder returns Filename: assets/Uploads/2011-Totally-Awesome-Awards/
Debug Image returns Filename: assets/Themes/Ahoy/ahoy.jpg

somepage.ss
<% if RotateImages %>
<% loop RotateImages %>
$Tag
<% end_loop %>
<% end_if %>
I am using a rotating image slider on the website and using this function to write the current list of image files.

Avatar
spierala

Community Member, 80 Posts

14 April 2013 at 5:28am

Hi!
what is displayed if you put $$Debug instead of $Tag in the template?
could you maybe also debug this:
Debug::show(DataObject::get("Image", "ParentID = '{$folder->ID}'"));

and are the Image Entries for sure created in the Database (in the File table)?
maybe not if you just uploaded the images without running /dev/tasks/FilesystemSyncTask

Florian

Avatar
acktivate

Community Member, 11 Posts

14 April 2013 at 6:35am

Hi - thank your for your reply.

I added $$Debug and received this:

Name:RotateImages
Table:
Value:

I also ran /dev/tasks/FilesystemSyncTask and it added the 5 files I had uploaded to the directory.

When I add Debug::show(DataObject::get("Image", "ParentID = '{$folder->ID}'")); I receive an error.

Go to Top