0

I am running script for importing products. For some of the skus it is giving ->

Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Invalid image file type.' in /home/oceanf7p/public_html/app/Mage.php:595 Stack trace:

/home/oceanf7p/public_html/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php(282):

Mage::throwException('Invalid image f...')

/home/oceanf7p/public_html/app/code/core/Mage/Catalog/Model/Product.php(1042):

Mage_Catalog_Model_Product_Attribute_Backend_Media->addImage(Object(Mage_Catalog_Model_Product), '/home/oceanf7p/...', Array, false, false)

/home/oceanf7p/public_html/import-products.php(341):

Mage_Catalog_Model_Product->addImageToMediaGallery('/home/oceanf7p/...',

Array, false, false) #3 {main} thrown in

/home/oceanf7p/public_html/app/Mage.php on line 595

I checked for image file extension also. its jpg only.

this is happening with some skus while others are imported successfully.

2
  • Share your code Mar 6, 2018 at 7:34
  • @ Raul : Check the image files with errors Files saved with .jpg extension are not always valid .jpg files How to check it?? I almost tried everything but now clueless.
    – User132887
    Mar 14, 2018 at 7:10

2 Answers 2

0

Check the image files with errors

Files saved with .jpg extension are not always valid .jpg files

0
$productSKU = $data[0];
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
$mySaveDir = Mage::getBaseDir('media') . DS . 'import' . DS ;

                    //$filename = basename($urlToImage);
$filename = $data[61];
$completeSaveLoc = $mySaveDir.$filename;
if(file_exists($completeSaveLoc))
{ 
    $ourProduct->setMediaGallery(array('images' => array(), 'values' => array()));//media gallery initialization
    $ourProduct->addImageToMediaGallery($completeSaveLoc, array('image', 'small_image', 'thumbnail'),false, false);
    $ourProduct->save();
    echo "done ";
}

if($data[62] != '')
{
    $extraimg = $data[62];
    $ourProduct1 = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
    $imagesaveloc = $mySaveDir.$extraimg;
    if(file_exists($imagesaveloc))
    {
        echo "file there";
        $ourProduct1->addImageToMediaGallery($imagesaveloc, null,false,false);
        $ourProduct1->save();

    }
}

if($data[63] != '')
{
    $extraimg2 = $data[63];
    $ourProduct2 = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
    $imagesaveloc = $mySaveDir.$extraimg2;
    if(file_exists($imagesaveloc))
    {
        echo "file there";
        $ourProduct2->addImageToMediaGallery($imagesaveloc, null,false,false);
        $ourProduct2->save();

    }
}

if($data[64] != '')
{
    $extraimg3 = $data[64];
    $ourProduct3 = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
    $imagesaveloc = $mySaveDir.$extraimg3;
    if(file_exists($imagesaveloc))
    {
        echo "file there";
        $ourProduct3->addImageToMediaGallery($imagesaveloc, null,false,false);
        $ourProduct3->save();

    }
}

if($data[65] != '')
{
    $extraimg4 = $data[65];
    $ourProduct4 = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
    $imagesaveloc = $mySaveDir.$extraimg4;
    if(file_exists($imagesaveloc))
    {
        echo "file there";
        $ourProduct4->addImageToMediaGallery($imagesaveloc, null,false,false);
        $ourProduct4->save();

    }
}

if($data[66] != '')
{
    $extraimg4 = $data[66];
    $ourProduct4 = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
    $imagesaveloc = $mySaveDir.$extraimg4;
    if(file_exists($imagesaveloc))
    {
        echo "file there";
        $ourProduct4->addImageToMediaGallery($imagesaveloc, null,false,false);
        $ourProduct4->save();

    }
}

I have done coding for 10 images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.