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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Need Help - Uploading and Displaying Multiple Images


Reply

2 Posts   319 Views

Avatar
BarcelonaV

22 July 2013 at 5:47am (Last edited: 22 July 2013 5:50am), Community Member, 4 Posts

Hello, I'm very new to SilverStripe and like the title says, I'm trying to upload multiple images at once and then be able to display them through my template. I haven't been able to get it to work how I want it to. I'm using SilverStripe 3.

The code for my ImagePage.php file is below where I create the form in the CMS, everything uploads to the "Images" folder I created within the assets folder just fine.

<?php

class ImagePage extends Page {

static $has_many = array(
"PageImages" => "Image"
);

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', $uploadField = new UploadField('PageImages'));
$fields->removeFieldFromTab('Root.Main', 'Content');

$uploadField->setFolderName('Images');

return $fields;
}

}

class ImagePage_Controller extends Page_Controller {

}

?>

The code for my template is as follows:

<% if PageImages %>
<% loop PageImages %>
<img src="$URL" alt="$Title" />
<% end_loop %>
<% end_if %>

The problem is that when I go to view the page that should be displaying the images, they don't display and instead have the "non existant/broken link" image in its place. When I inspect the element it shows that the url being supplied by $URL is '/silverstripe/assets/image1.jpg instead of what I want, '/silverstripe/assets/images/image1.jpg'.

I can't seem to find anything that simply explains how to display multiple images from within a specific folder. I would greatly appreciate it if someone could explain the steps necessary to making this work for me.

Avatar
BarcelonaV

22 July 2013 at 11:00am Community Member, 4 Posts

Resolved my issue.

After hours of Google searches and forum digging, turns out that the segment:

"static $has_many = array(

"PageImages" => "Image"

);"

should be:

"static $many_many = array(

"PageImages" => "Image"

);"

Hopefully this will save other newbies the trouble I went through!