0

申し訳ありませんが、私は php の新しい学習者です。次のコードは、製品ごとに複数の画像をインポートできます。インポート ファイルに「ギャラリー」列を追加し、各画像をセミコロンで区切るだけです。 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) {}   

ありがとうございました。

4

1 に答える 1

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) {}

注:これにより、ファイルをチェックするたびにサーバーにアクセスする必要があるため、プロセスが大幅に遅くなります。

于 2012-12-28T02:01:02.333 に答える