0

I am facing Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Image does not exist.'

<pre>
<?php
set_time_limit(0);
ini_set('memory_limit', '1024M');
include_once "app/Mage.php";
include_once "downloader/Maged/Controller.php";

Mage::init();

$app = Mage::app('default');

//The category names should be exactly the same name from the csv file where the id is the corresponding category id in magento. This is done when the csv file doesn't contain ids for categories but the name of categories.
$categories = array(
    'Category 1' => 3,
    'Category 2' => 4,
    'Category 3' =>5,
    'Category 4'=>6,


);
$row = 0;

if (($handle = fopen("trem2.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        echo 'Importing product: '.$data[0].'<br />';
        foreach($data as $d)
        {
            echo $d.'<br />';
        }
        $num = count($data);
        //echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;

        if($row == 1){ continue;}

           $product = Mage::getModel('catalog/product');

        $product->setSku($data[0]);
        $product->setName($data[3]);
        $product->setDescription($data[6]);
        $product->setShortDescription('');
        $product->setManufacturer($data[20]);
        $product->setPrice($data[9]);
        $product->setTypeId('simple');


        $fullpath = 'media/catalog/product/thumb/';
        $ch = curl_init ($data[14]);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $rawdata=curl_exec($ch);
        curl_close ($ch);
       echo  $fullpath = $fullpath.$data[2].'.jpg';
        if(file_exists($fullpath)) {
            unlink($fullpath);
        }
        $fp = fopen($fullpath,'x');
        fwrite($fp, $rawdata);
        fclose($fp);
       $product->addImageToMediaGallery($fullpath, 'thumbnail', false);

        $fullpath = 'media/catalog/product/small/';
        $ch = curl_init ($data[2]);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $rawdata=curl_exec($ch);
        curl_close ($ch);
        $fullpath = $fullpath.$data[2].'.jpg';
        if(file_exists($fullpath)) {
            unlink($fullpath);
        }
        $fp = fopen($fullpath,'x');
        fwrite($fp, $rawdata);
        fclose($fp);
       $product->addImageToMediaGallery($fullpath, 'small-image', false);

        $fullpath = 'media/catalog/product/high/';
        $ch = curl_init ($data[2]);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        $rawdata=curl_exec($ch);
        curl_close ($ch);
        echo $data[2];
        echo $fullpath = $fullpath.$data[2].'.jpg';
        if(file_exists($fullpath)) {
            unlink($fullpath);
        }
        $fp = fopen($fullpath,'x');
        fwrite($fp, $rawdata);
        fclose($fp);
        $product->addImageToMediaGallery($fullpath, 'image', false);



        $product->setAttributeSetId(4); // need to look this up
        $product->setCategoryIds(array($categories[$data[11]])); // need to look these up
        $product->setWeight(0);
        $product->setTaxClassId(2); // taxable goods
        $product->setVisibility(4); // catalog, search
        $product->setStatus(1); // enabled

        // assign product to the default website
        $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));

     $product->save();      

$stockItem = Mage::getModel('cataloginventory/stock_item');
            $stockItem->assignProduct($product);
            $stockItem->setData('is_in_stock', 1);
            $stockItem->setData('stock_id', 1);
            $stockItem->setData('store_id', 1);
            $stockItem->setData('manage_stock', 1);
            $stockItem->setData('use_config_manage_stock', 0);
            $stockItem->setData('min_sale_qty', 1);
            $stockItem->setData('use_config_min_sale_qty', 0);
            $stockItem->setData('max_sale_qty', 1000);
            $stockItem->setData('use_config_max_sale_qty', 0);
            $stockItem->setData('qty', $data[4]);
            $stockItem->save();




        $product->save();    


    }
    fclose($handle);

}


?>
</pre>

Or what is the other way to import?

Thanks.!

  • what issue .. yum have facing – Amit Bera Jan 16 '17 at 7:08
  • @AmitBera he is getting : Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Image does not exist.' – Baby in Magento Jan 16 '17 at 7:10
  • 1
    @AmitBera I am facing Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Image does not exist.' while import products programmatically using the above code... – Vibhanshu Sharma Jan 16 '17 at 9:14
  • if nothing worked for you, you can go with magmi – Baby in Magento Jan 16 '17 at 13:18
1

I dont understand why you use there unlink , it will delete your image and thats why you get image not find error

    $fullpath = 'media/catalog/product/thumb/';
    $ch = curl_init ($data[14]);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);
   echo  $fullpath = $fullpath.$data[2].'.jpg';
    /*if(file_exists($fullpath)) {
        unlink($fullpath);
    }*/ //comment this code and check 
    $fp = fopen($fullpath,'x');
    fwrite($fp, $rawdata);
    fclose($fp);
  • Thanks , now the image file uploaded but uploaded image got 0 bytes. – Vibhanshu Sharma Jan 16 '17 at 12:35

Your Answer

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

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