Answer the question
In order to leave comments, you need to log in
How to implement multiple file upload in bitrix:form?
Hello !
There is a web form on Bitrix, which is displayed using bitrix:form. You need to somehow implement multiple file uploads. There is no such possibility in the form setup. Tell me where to dig.
Answer the question
In order to leave comments, you need to log in
Well, if you do it in the standard way, then you can only make a lot of file-type answers. and on the form it will be displayed in separate fields and separately cling files to them.
In my case, this method worked:
I created a lot of file type answers in the question.
And at a form output I do not display them. Instead of these inputs, I display mine with name="files"
<input type="file" id="files" name="files[]" multiple>
AddEventHandler("form", "onBeforeResultAdd", ['FormResultAdd', 'before']);
class FormResultAdd
{
/**
* Вернет input name для ответов типа file вопроса fieldCode
* @param $formId
* @param string $fieldCode
* @return array
*/
public static function getFilesInputNames($formId, $fieldCode = 'FILES'){
$res = [];
if($question = \CFormField::GetBySID($fieldCode, $formId)->Fetch()){
$by = 's_id';
$order = 'asc';
$filter = false;
if(intval($question['ID'])){
$rsAnswers = \CFormAnswer::GetList($question['ID'], $by,$order,["FIELD_TYPE"=>'file'], $filter );
while ($arAnswer = $rsAnswers->Fetch())
{
$res[]='form_file_'.$arAnswer['ID'];
}
}
}
return $res;
}
/**
* Осуществляем загрузку множества файлов через один input type="file" multiple
* так как стандартными средствами Битрикс можно сделать только через множество input type="file"
* @param $WEB_FORM_ID
* @param $arFields
* @param $arrVALUES
* @throws Main\SystemException
*/
public static function loadMultiple($WEB_FORM_ID, &$arFields, &$arrVALUES) {
global $_FILES;
if($_FILES['files']){
$files = [];
if(is_array($_FILES['files']['name'])){
foreach ($_FILES['files'] as $key => $val){
foreach ($val as $k => $v){
$files[$k][$key]= $v;
}
}
}else{
$files = [$_FILES['files']];
}
unset($_FILES['files']);
$err = [];
if($inputsName = self::getFilesInputNames($WEB_FORM_ID)){
foreach ($files as $f){
if($inputName = array_shift($inputsName)){
$_FILES[$inputName] = $f;
}else{
$err[] = $f;
}
}
if($err){
$firelds = [
'TITLE' => 'Не хватило полей в форме для подгрузки файлов '.__FUNCTION__,
'MESSAGE' => print_r($err, true),
];
\CEvent::Send('DEBUG_SEND', SITE_ID, $firelds);
}
}
}
return true;
}
public static function before($WEB_FORM_ID, &$arFields, &$arrVALUES) {
self::loadMultiple($WEB_FORM_ID, $arFields, $arrVALUES);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question