F
F
F(x)2020-02-05 09:20:59
1C-Bitrix
F(x), 2020-02-05 09:20:59

How to add pictures to infoblock via API?

How to add (upload) pictures to the infoblock via postman?
wrote this code:

$targetDir = $_SERVER["DOCUMENT_ROOT"]."/upload/tmp/";
    $allowTypes = array('jpg','png','jpeg','gif');
    $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}";
    $statusMsg = $errorMsg = $arrPhoto = $errorUpload = $errorUploadType = $arrTmp = [];
    if(!empty(array_filter($_FILES['photo']['name']))) {
        foreach ($_FILES['photo']['name'] as $key => $val) {
            // File upload path
            $fileName = basename($_FILES['photo']['name'][$key]);
            $targetFilePath = $targetDir . $fileName;
            $file_info = $_FILES["photo"]["tmp_name"];

            // Check whether file type is valid
            $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
            echo json_encode($fileType);
            if (in_array($fileType, $allowTypes)) {
                // Upload file to server
                if (move_uploaded_file($_FILES["photo"]["tmp_name"][$key], $targetFilePath)) {
                    $file_info = $_SERVER["DOCUMENT_ROOT"]."/upload/tmp/".$file_info;
                    $prop["photo"]["n".count($prop["photo"])]["VALUE"] = $file_info;
                    $arrPhoto[] = $url . $targetFilePath;
                } else {
                    $errorUpload[] = $_FILES['photo']['name'][$key];
                }
            } else {
                $errorUploadType[] = $_FILES['photo']['name'][$key];
            }
        }
    }

$prop = array(
"photo" => $arrPhoto
);


it is written to a local file, but does not show in the admin panel.
How to load the built-in Bitrix method?
Can you tell me how to upload images via postman?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail, 2020-02-05
@RuComMarket

it is necessary to save the file using Bitrix tools,
my old version:

$file_im = CFile::CheckImageFile($_FILES["PREVIEW_PICTURE"],0,0,0,"IMAGE");
    if (!$file_im) {
      CAllFile::ResizeImage(
        $_FILES["PREVIEW_PICTURE"],
        array(
         "width" => "800",
         "height" => "800"
        ),
        BX_RESIZE_IMAGE_PROPORTIONAL_ALT
        );
      $file_ID = CFile::SaveFile($_FILES["PREVIEW_PICTURE"], "catalog/".$type_object);
    } else {
      $return["error"]["str"] = $file_im;
      exit(\Bitrix\Main\Web\Json::encode($return));
    }
    if ($file_ID) $file_pp = CFile::MakeFileArray($file_ID);

and then you hammer into the desired property of the infoblock, for example PREVIEW_PICTURE, i.e. first you save, the id comes in the response, you get the desired array from it, which you drive into the desired field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question