Advertisement
animdenis

Upload collection img

May 15th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. //title: Присваивание картинок
  2. \Bitrix\Main\Loader::includeModule('iblock');
  3.  
  4. function rbsCheckDir($dirPath, $dirName)
  5. {
  6.     $collectionDir = $_SERVER["DOCUMENT_ROOT"]."/upload/collection_images/";
  7.     return is_dir($collectionDir.$dirPath) && $dirName != '.' && $dirName != '..';
  8. }
  9.  
  10. $collectionDirSourse = $_SERVER["DOCUMENT_ROOT"]."/upload/collection_images/";
  11. $ib = 17;
  12. $dir = opendir($collectionDirSourse);
  13. $arLog = [];
  14. while($brand = readdir($dir))
  15. {
  16.     if(rbsCheckDir($brand, $brand))
  17.     {
  18.         $brandDb = CIblockSection::GetList(['ID' => 'asc'], ['IBLOCK_ID' => $ib, '=NAME' => $brand], false, ['ID', 'NAME']);
  19.         $brandDir = opendir($collectionDirSourse.$brand);
  20.  
  21.         while($obBrand = $brandDb->GetNext())
  22.         {
  23.             $collectionImg = false;
  24.             while($collection = readdir($brandDir))
  25.             {
  26.                 if(rbsCheckDir($brand.'/'.$collection, $collection))
  27.                 {
  28.                     $collDb = CIblockSection::GetList(['ID' => 'asc'], ['IBLOCK_ID' => $ib, '=NAME' => $collection, 'IBLOCK_SECTION_ID' => $obBrand['ID']], false, ['ID', 'NAME']);
  29.                     $collectionDir = opendir($collectionDirSourse.$brand.'/'.$collection.'/web/');
  30.                     while($obCol = $collDb->GetNext())
  31.                     {
  32.                         while($image = readdir($collectionDir))
  33.                         {
  34.                             if(strpos($image, '.jpg') !== false)
  35.                             {
  36.                                 $dbElem = CIblockElement::GetList([],
  37.                                     ['IBLOCK_ID' => $ib, '=CODE' => str_replace('.jpg', '', $image), 'IBLOCK_SECTION_ID' => $obCol['ID']], false, false,
  38.                                     ['ID', 'NAME', 'CODE']);
  39.                                 if($obElem = $dbElem->GetNext())
  40.                                 {
  41.                                     $el = new CIBlockElement;
  42.                                     $picPath = $collectionDirSourse.$brand.'/'.$collection.'/web/'.$image;
  43.                                     $fileArray = CFile::MakeFileArray($picPath);
  44.                                     $arLoadProductArray = array(
  45.                                         "PREVIEW_PICTURE" => $fileArray,
  46.                                         "DETAIL_PICTURE" => $fileArray,
  47.                                     );
  48.                                     //print_r($fileArray);
  49.                                     if(!$el->Update($obElem['ID'], $arLoadProductArray))
  50.                                         $arLog[$obElem['ID']] = $el->LAST_ERROR;
  51.                                 }
  52.                             }
  53.                         }
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.    }
  59. }
  60.  
  61. print_r($arLog);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement