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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

Auto-update image upload directory

Go to End

3 Posts   1042 Views


Community Member, 2 Posts

3 December 2012 at 7:31am

Hi all,

I've hit a bit of a snag which I'm sure has a simple solution, but I can't for the life of me figure it out. I have a page with an image upload field, and I need the upload directory to be /[parent page->URLSegment]/[URLSegment]/. At the moment I'm doing this with UploadField->setFolderName() in getCMSFields(), but this of course only changes the upload directory when the page is saved. Ideally I want it to change as soon as the URLSegment changes, just like the URLSegment does when the Title is changed.



Community Member, 17 Posts

4 December 2012 at 11:36am

You should be able to do this in the 'onBeforeWrite()' functions of the different pages.

Have a look at this code:

	public function onBeforeWrite() {

		if($this->isChanged('Title')) {
			$this->URLSegment = $this->createUniqueURLSegement();

		if($this->isChanged('URLSegment')) {
			$fields = $this->getChangedFields();
			$URLSegment = $fields['URLSegment'];
			$pdfFolder = Folder::find(self::$pdf_folder . '/' . $URLSegment['before']);
			if($pdfFolder) {
			$imageFolder = Folder::find(self::$image_folder . '/' . $URLSegment['before']);
			if($imageFolder) {

Here I do exactly what you want and it seems to work. Basicaly i have several AnnualProgramms with several events. So every time the title of an annualprogramm changes i change the URLSegment and remane all folders which are affected by the renaming.


Community Member, 2 Posts

14 December 2012 at 6:35pm

Finally got a chance to try this out and it works perfectly, thanks!