我正在尝试通过CSV实用地上传产品,所有脚本运行良好,但我只有1个问题,我的CSV文件中有3列图像。第1幅图像可用,但在第2列和第3列中,有些图像缺少名称,但存在图像在目录中找不到,但是当我运行脚本时,仅保存一个图像,其余图像不保存
我的代码是
<?php
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager1 = Magento\Framework\App\ObjectManager::getInstance();
$directoryList = $objectManager1->get('\Magento\Framework\App\Filesystem\DirectoryList');
$path = $directoryList->getPath('media');
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$myarray = glob("test.csv");
usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
if(count($myarray)){
$csv_map = array_map('str_getcsv', file($myarray[count($myarray)-1]));
array_walk($csv_map, function(&$a) use ($csv_map) {
$a = array_combine($csv_map[0], $a);
});
array_shift($csv_map);
$message = '';
$count = 1;
foreach($csv_map as $data){
//echo '<pre>';print_r($data);exit;
$product = $objectManager->create('Magento\Catalog\Model\Product');
$product->setName(trim($data['NAME']));
$product->setTypeId('simple');
$product->setAttributeSetId(4);
$product->setSku(trim($data['MODEL NUMBER']));
$product->setUrlPath(trim($data['url']).trim($data['MODEL NUMBER']).('zenith'));
$product->setWebsiteIds(array(1));
$product->setVisibility(4);
$product->setBrand(trim($data['BRAND']));
$product->setFunctions(trim($data['FUNCTIONS']));
$product->setPowerReserve(trim($data['POWER RESERVE']));
$product->setStrapType(trim($data['STRAP TYPE']));
$product->setWarranty(trim($data['WARRANTY']));
$product->setFamily(trim($data['FAMILY']));
$product->setWaterResistance(trim($data['WATER RESISTANCE']));
$product->setCreatedAt(strtotime('now'));
$product->setPrice(trim($data['PRICE']));
$product->setcaseSize(trim($data['CASE SIZE']));
//$product->setColor(trim($data['dial_colour']));
$product->setMovement(trim($data['MOVEMENT']));
$product->setCaseShape(trim($data['CASE SHAPE']));
//$product->setGender(trim($data['gender']));
$product->setCaseMaterial(trim($data['CASE MATERIAL']));
//$_product->setShortDescription(trim($data['Short Description'])); // add text attribute
//$_product->setDescription(trim($data['Long Description'])); // add text attribute
$img_url = trim($data['IMAGE1']);
$img_url1=trim($data['IMAGE2']);
$img_url2=trim($data['IMAGE3']);
$dir = $directoryList->getPath('media').'/big/';
$imgpath = $dir.$img_url;
$imgpath1 = $dir.$img_url1;
$imgpath2 = $dir.$img_url2;
$product->addImageToMediaGallery($imgpath, array('image'), false, false);
if (file_exists($dir.$img_url1)) {
$product->addImageToMediaGallery($imgpath1, array('thumbnail'), false, false);
}
if (file_exists($dir.$img_url2)) {
$product->addImageToMediaGallery($imgpath2, array('small_image'), false, false);
}
$product->setStockData(array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => 1, //Stock Availability
'qty' => 100 //qty
)
);
$product->save();
}
echo'sucess';
}
?>
请在图像部分建议我做错了
答案 0 :(得分:0)
检查图像是否存在 @exif_imagetype($ image_path)。 谢谢