I
I
Ivan Kasatkin2021-07-12 13:44:19
1C-Bitrix
Ivan Kasatkin, 2021-07-12 13:44:19

How to add a picture to MORE_PHOTO?

The situation is as follows:
There is an assortment of goods from 904 elements, I was told that it is necessary to attach one photo to each.
As you understand, doing it with your hands is very painful.
I don't understand what script to write to add another photo to this property.
Thank you all in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2021-07-12
@Dublet

In general, here is the code for adding a picture to MORE_PHOTO without overwriting the old ones, I tested it on bitrixlabs, it is working.

\Bitrix\Main\Loader::includeModule('iblock');

$iBlockId = 28;
$file = CFile::MakeFileArray($_SERVER['DOCUMENT_ROOT'] . "/backs/doorman.jpg");
$result = CIblockElement::GetList([], ['IBLOCK_ID' => $iBlockId, 'INCLUDE_SUBSECTIONS' => 'Y'], false, false, ['ID']);
while ($element = $result->Fetch()) {
    $values = ['n0' => ['VALUE' => $file, 'DESCRIPTION' => '']];
    $result2 = CIBlockElement::GetProperty($iBlockId, $element['ID'], [], ['CODE' => 'MORE_PHOTO']);
    while ($photo = $result2->Fetch()) {
        $values[$photo['PROPERTY_VALUE_ID']] = ['VALUE' => [
            'name' => '',
            'type' => '',
            'tmp_name' => '',
            'error' => 4,
            'size' => 0,
            'description' => ''
        ], 'DESCRIPTION' => ''];
    }

    CIBlockElement::SetPropertyValuesEx($element['ID'], $iBlockId, ['MORE_PHOTO' => $values]);
}

T
tgarl, 2021-07-12
@tgarl

$elementId=1; //ID элемента в который нужно добавить
$tmpFilePath=$_SERVER['DOCUMENT_ROOT']."/upload/picatest.jpg";
$arFile=array("VALUE" => \CFile::MakeFileArray($tmpFilePath),"DESCRIPTION"=>"");
\CIBlockElement::SetPropertyValueCode($elementId, 'MORE_PHOTO', $arFile);

with such an update, he does not delete the pictures if they were, but adds
if you need several pictures to the element,
then we collect everything in the $arFile array (example without cycles and other things)
$elementId=1; //ID элемента в который нужно добавить

$tmpFilePath=$_SERVER['DOCUMENT_ROOT']."/upload/picatest1.jpg";
$arFile[]=array("VALUE" => \CFile::MakeFileArray($tmpFilePath),"DESCRIPTION"=>"");

$tmpFilePath=$_SERVER['DOCUMENT_ROOT']."/upload/picatest2.jpg";
$arFile[]=array("VALUE" => \CFile::MakeFileArray($tmpFilePath),"DESCRIPTION"=>"");

$tmpFilePath=$_SERVER['DOCUMENT_ROOT']."/upload/picatest3.jpg";
$arFile[]=array("VALUE" => \CFile::MakeFileArray($tmpFilePath),"DESCRIPTION"=>"");


\CIBlockElement::SetPropertyValueCode($elementId, 'MORE_PHOTO', $arFile);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question