SilverStripe 4 // UploadField // target folder // always created // how to avoid empty subfolders?

Hi there,

I am using SilverStripe 4 (4.13).
I want to organize my uploaded asset files in subfolders (using the id of the data object they belong to).

It is easy using “setFolderName(‘my/custom/subfolder/$ID’)” on SilverStripe\AssetAdmin\Forms\UploadField.

But the folder is always created, even if I do not upload a file.
So this results in polluting the uploads directory with many empty subfolders.

Is there a way to create the folder only on demand (if there is actual a file uploaded)?
Or is there some kind of an after upload hook?

Thank you

1 Like

Maybe you can work with the module silvershop/silverstripe-hasonefield that allows to manage dynamically a $has_one relationship, create a $has_one Image::class or $has_one Gallery::class (to implement) then only create the folder at the creation of the Image or Gallery.