Hello,
I was looking for a way to do exactly this (storing images in different subfolders depending of say for instance, a dropdown with subfolders), and I think I found the reason why you replace the image with every subsequent upload.
You need to seperate the the images from the rest of your records and create a relationship between the two tables like this:
Units:
Unit_Id
Unit_Title
...
Images
Image_Id
Image_File (stores the filename)
Unit_Key
Then you create a relationship in relationships.ini file in your folder for the Units that looks like this:
[Images]
Images.Unit_Key="$Unit_Id"
In your fields.ini for that table you add:
[Images]
widget:label = "Images"
transient=1
relationship=Images
widget:type=grid
widget:columns="Image_File"
This will setup a grid that allows you to upload multiple images from your units table directly into the table for your images, and retrieve them as well
Next step is to actually tell that grid to allow for IMAGES to be written.
This can be achieved by adding the following configuration to the fields.ini of your Images table
[Image_File]
Type = container
widget:type = ajax_upload
logo=1
savepath = %I use a realtive path here, that ends with /table_name%
url = %Same here, but due to my setup I prefer to add here the FULL url, so starting with https://, might be needed in your case as well to allow to go all the way to root level%
allowed_mimetypes = image/gif,image/png,image/jpg,image/jpeg,image/webp
allowed_extensions = jpg,jpeg,webp,gif,png
disallowed_mimetypes = application/json,application/xml,application/exe
disallowed_extensions = exe
Now that I am writing this out, I believe I see a possible solution for our use case:
by Creating a text field in the Images table, concatenating that to both the savepath and url directives here, this should be possible (Given both the parent and subfolder exist and have the needed permission)
It would then Look a bit like this in your images fields.ini:
[Image_Category]
widget:type = text
[Image_File]
Type = container
widget:type = ajax_upload
logo=1
savepath = %I use a realtive path here, that ends with /table_name%/"$Image_Category"
url = %Same here, but due to my setup I prefer to add here the FULL url, so starting with https://, might be needed in your case as well to allow to go all the way to root level%/"$Image_Category"
allowed_mimetypes = image/gif,image/png,image/jpg,image/jpeg,image/webp
allowed_extensions = jpg,jpeg,webp,gif,png
disallowed_mimetypes = application/json,application/xml,application/exe
disallowed_extensions = exe
and of course you would need to add the Images_Category to the grid in your transient field in the parent table configuration.
I hope this made any sense, I will try out the solution with multiple subdirectories and post here again whether or not my tests are successful.
Ruben