3066 Posts in 866 Topics by 648 members
| Go to End | Next > | |
| Author | Topic: | 2327 Views |
-
List images from upload directory

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" />
-
Re: List images from upload directory

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.
-
Re: List images from upload directory

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 -
Re: List images from upload directory

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
-
Re: List images from upload directory

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 -
Re: List images from upload directory

14 April 2013 at 4:34am Last edited: 14 April 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.jpgsomepage.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. -
Re: List images from upload directory

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/FilesystemSyncTaskFlorian
-
Re: List images from upload directory

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.
| 2327 Views | ||
| Go to Top | Next > |




