Jump to:

3450 Posts in 1063 Topics by 738 members

Data Model Questions

SilverStripe Forums » Data Model Questions » List images from upload directory

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

Page: 1 2
Go to End
Author Topic: 4542 Views
  • Teere
    Avatar
    Community Member
    1 Post

    List images from upload directory Link to this post

    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" />

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: List images from upload directory Link to this post

    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.

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: List images from upload directory Link to this post

    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

  • lerni
    Avatar
    Community Member
    80 Posts

    Re: List images from upload directory Link to this post

    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

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: List images from upload directory Link to this post

    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

  • acktivate
    Avatar
    Community Member
    11 Posts

    Re: List images from upload directory Link to this post

    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.

  • spierala
    Avatar
    Community Member
    80 Posts

    Re: List images from upload directory Link to this post

    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

  • acktivate
    Avatar
    Community Member
    11 Posts

    Re: List images from upload directory Link to this post

    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.

    4542 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.