i have custom import script which i use to import products(from excel sheets), it works just fine, although on some products it gives error regarding image. Script runs as cronjob since manufacturers upload their feeds at different time

the error which i get is coming from this code:

$pathinfo = pathinfo($file);
$imgExtensions = ['jpg', 'jpeg', 'gif', 'png'];
if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) {
    throw new LocalizedException(
       __('The image type for the file is invalid. Enter the correct image type and try again.')

code for image part is this:

/** var $directoryList \Magento\Framework\App\Filesystem\DirectoryList **/
$dir = $this->_directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA) . DIRECTORY_SEPARATOR .'import'. DIRECTORY_SEPARATOR .$manufacturer;

$it = new \RecursiveDirectoryIterator($dir);
$display = Array('jpeg', 'jpg');
foreach(new \RecursiveIteratorIterator($it) as $path){
    $fileName = explode('.', $path->getFileName());
    if (in_array(strtolower(array_pop($fileName)), $display)){
        if(strpos($path->getPathName(), trim($workSheet->getCell('O'.$row)->getValue())) !== false){
            $product->addImageToMediaGallery($path->getPathName(), array('image', 'small_image', 'thumbnail'), false, false);
        if(strlen(trim($workSheet->getCell('P'.$row)->getValue())) > 0 && strpos($path->getPathName(), trim($workSheet->getCell('P'.$row)->getValue())) !== false){
            $product->addImageToMediaGallery($path->getPathName(), array('image', 'small_image'), false, false);
        if(strlen(trim($workSheet->getCell('Q'.$row)->getValue())) > 0 && strpos($path->getPathName(), trim($workSheet->getCell('Q'.$row)->getValue())) !== false){
            $product->addImageToMediaGallery($path->getPathName(), array('image', 'small_image'), false, false);

this part searches directory for images and if file name is matched with line in excel sheet it will set it as product image.

have checked file mime type it's image/jpeg

the script stops on some products with this error.

manually setting images is not an option since script is also making grouped products:

make product 1 - if product is marked as grouped, scripts adds id of created product and continues to next line in excel if we get image error at this point the next stage where we group both products is skipped since script is halted.

tried following to ignore the error:

try {
    $product->addImageToMediaGallery($path->getPathName(), array('image', 'small_image'), false, false);
} catch(\Exception $e){

but no luck in cron_schedule table i see same error.

any solution to my problem ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.