I
I
I'm Yoda2019-02-12 16:00:17
1C-Bitrix
I'm Yoda, 2019-02-12 16:00:17

How to add a file to a custom field "file"?

Hello!
I'm making a file uploader. I save downloads in HL.
To save a file, I use the CFile::SaveFile method . The file is downloaded and the ID of that file is returned to me.

if (is_uploaded_file($_FILES["file"]["tmp_name"])) {

                $arFileArray = array(
                    "name" => $_FILES["file"]["name"],
                    "size" => $_FILES["file"]["size"],
                    "tmp_name" => $_FILES["file"]["tmp_name"],
                    "type" => $_FILES["file"]["type"],
                    "MODULE_ID" => "highloadblock"
                );

                $FileID = CFile::SaveFile($arFileArray, "highloadblock");
            }

How to write the received ID or array into a custom field of the file type?
$hlblock_id = 1;
            $hlblock = HL\HighloadBlockTable::getById($hlblock_id)->Fetch();
            $entity = HL\HighloadBlockTable::compileEntity($hlblock);
            $entity_data_class = $entity->getDataClass();

            $result = $entity_data_class::add(
                "UF_UPLOAD_FILE" => $FileID
            );

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
I'm Yoda, 2019-02-17
@Anadi

"UF_UPLOAD_FILE" => \CFile::MakeFileArray($FileID)

I
Ilya, 2019-02-12
@jasper-blondin

Bitrix has glitches with the File field type in HL blocks. The value cannot be saved.
I solved the problem head-on: I gave the field with the file the type "Number" (you can give the type "String") and wrote down the file ID there without any problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question