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.

General Questions /

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

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

Need Help - Uploading and Displaying Multiple Images


Go to End


2 Posts   1232 Views

Avatar
BarcelonaV

Community Member, 4 Posts

22 July 2013 at 5:47am

Edited: 22/07/2013 5:50am

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

Community Member, 4 Posts

22 July 2013 at 11:00am

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!