Answer the question
In order to leave comments, you need to log in
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
);
Answer the question
In order to leave comments, you need to log in
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question