首页 > 其他 > 详细

magento 删除冗余产品图片

时间:2014-12-24 14:55:16      阅读:643      评论:0      收藏:0      [点我收藏+]
/*
 
If your products have been deleted but not your media gallery images you 
can use the following SQL statement to remove these dormant records.
 
Please make sure you backup your installation and database before running this 
SQL statement. It has been used on Mage 1.8.1 system: 
 
DELETE `img` FROM `catalog_product_entity_media_gallery` img 
LEFT JOIN `catalog_product_entity` AS prod ON img.entity_id = prod.entity_id
WHERE prod.sku IS NULL
 
 */
 
try {
 
        $sMageFile = __DIR__ . ‘app/Mage.php‘;
        if(!is_file($sMageFile)) {
                throw new Exception(‘Cannot find: ‘ . $sMageFile);
        }
        require_once($sMageFile);
        $oApp = Mage::app();
 
        // Set area to admin
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 
        $sImgDir = Mage::getBaseDir(‘media‘) . DS . ‘catalog‘ . DS . ‘product‘;
 
        $oResource = Mage::getSingleton(‘core/resource‘);
        $sMediaTbl = $oResource->getTableName(‘catalog_product_entity_media_gallery‘);
        $oReadConn = $oResource->getConnection(‘core_read‘);
 
        $i=0;
        $oIterator = new RecursiveDirectoryIterator($sImgDir);
        foreach( new RecursiveIteratorIterator($oIterator) as $sFile) {
 
                if(strpos($sFile, "/cache") !== false || is_dir($sFile) ) {
                        continue;
                }
 
                $sFilePath      = str_replace($sImgDir, "", $sFile);
                $sQuery         = ‘SELECT value FROM ‘ . mysql_real_escape_string($sMediaTbl) . ‘ WHERE value="‘ . mysql_real_escape_string($sFilePath) . ‘"‘;
                $sValue         = $oReadConn->fetchOne($sQuery);
 
                if($sValue == false){
                        echo "## REMOVEING: " . $sFilePath . " ## \n";
                        unlink($sFile);
                        $i++;
                }
        }
 
        echo "\r\n\r\n Finished removing $i  un-used product images\r\n\r\n";
 
} catch (Exception $e) {
        echo ‘<pre style="color:red;">‘ . $e->getMessage() . ‘</pre>‘;
}


magento 删除冗余产品图片

原文:http://my.oschina.net/liufeng815/blog/359975

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!