2012-12-28 94 views
0

對不起,我是php的新學員。以下代碼允許您爲每個產品導入多個圖像。只需在導入文件中添加一個「圖庫」列,然後用分號.import分隔每個圖像來自csv文件的數據。但有一個錯誤。如果我已經導入了圖像,當我再次導入時,它仍然會導入。但是,該產品有許多重複的圖像。如果產品已導入圖像,如何防止再次導入。如何防止重複導入圖像?

try { 
        $galleryData = explode(';',$importData["gallery"]); 
        foreach($galleryData as $gallery_img) 
        /** 
        * @param directory where import image resides 
        * @param leave 'null' so that it isn't imported as thumbnail, base, or small 
        * @param false = the image is copied, not moved from the import directory to it's new location 
        * @param false = not excluded from the front end gallery 
        */ 
        { 
          $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, null, false, false); 
        } 
       } 
      catch (Exception $e) {} 

謝謝。

回答

1

檢查看看file_exists()。如果不導入:

try { 
    $galleryData = explode(';',$importData["gallery"]); 
    foreach($galleryData as $gallery_img){ 

     if(!file_exists(Mage::getBaseDir('media') . DS . 'import' . $gallery_img)){ 
      $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, null, false, false); 
     } 

    } 
}catch (Exception $e) {} 

注:這將大幅提升,延緩下跌過程,因爲它有每次檢查該文件伸手到服務器。

+0

謝謝,但現在。它不會導入圖像。圖像導入不會運行一次 – hehe

+0

您可能需要檢查'if'語句的語法 –

+0

我很抱歉,我複製了您的代碼。 – hehe