In SS4,
I want to create a custom form in the front end with a field and this field can upload multiple files. But now, I can’t see any solution for this purpose.
I used UploadField, but I can’t get any data form to submit action.
I also used FileField, but its only support upload 1 file.
Please kindly take a look and let me know any solution for my issue.
Thank you very much,
You’ll want to use a FileField
with the “multiple” attribute. The name of the field must also contain brackets: (“Files”).
$Images = FileField::create('Images[]', 'Images')
->setAttribute('multiple', 'multiple');
File upload arrays are also strangely formatted in PHP. I recommend using a function to re-format the post data in your processor method, similar to the methods described by users on the PHP site. Here is the version that I used:
/**
* Re-arrange the $_FILES array to be more like $_POST
* Allows for multiple file uploads to be handled more easily
*
* Example:
* $_FILES['field']['name'][$i] becomes $_FILES['field'][$i]['name']
*
* @param array $filePostArray
* @return array
*/
function reArrayFiles($filePostArray)
{
$isMulti = is_array($filePostArray['name']);
$fileCount = $isMulti ? count($filePostArray['name']) : 1;
$fileKeys = array_keys($filePostArray);
$newFileArray = [];
for ($i = 0; $i < $fileCount; $i++) {
foreach ($fileKeys as $key) {
if ($isMulti) {
$newFileArray[$i][$key] = $filePostArray[$key][$i];
} else {
$newFileArray[$i][$key] = $filePostArray[$key];
}
}
}
return $newFileArray;
}
I used bigfork/silverstripe-dropzone and evaluated lekoala/silverstripe-filepond.
These are modern, JS-driven form fields that handle upload of multiple files and save them in your model.