cancel
Showing results for 
Search instead for 
Did you mean: 

failed to open stream: Permission denied in magento duplicate image removing code

failed to open stream: Permission denied in magento duplicate image removing code

<?php
include('app/Mage.php');
//Mage::App('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
error_reporting(E_ALL | E_STRICT);
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
ob_implicit_flush (1);

$mediaApi = Mage::getModel("catalog/product_attribute_media_api");
$_products = Mage::getModel('catalog/product')->getCollection();
$i =0;
$total = count($_products);
$count = 0;
foreach($_products as $_prod)
{
    $_product = Mage::getModel('catalog/product')->load($_prod->getId());
    $_md5_values = array();

    //protected base image
    $base_image = $_product->getImage();
    if($base_image != 'no_selection')
    {
        $filepath =  Mage::getBaseDir('media') .'/catalog/product' . $base_image  ;
    
        if(file_exists($filepath))
            $_md5_values[] = md5(file_get_contents($filepath));
    }

    $i ++;
    echo "rn processing product $i of $total ";

    // Loop through product images
    $_images = $_product->getMediaGalleryImages();
    if($_images){
        foreach($_images as $_image){
            //protected base image
            if($_image->getFile() == $base_image)
                continue;

            $filepath =  Mage::getBaseDir('media') .'/catalog/product' . $_image->getFile()  ;
            if(file_exists($filepath))
                $md5 = md5(file_get_contents($filepath));
            else
                continue;

            if(in_array($md5, $_md5_values))
            {
                $mediaApi->remove($_product->getId(),  $_image->getFile());
                echo "rn removed duplicate image from ".$_product->getSku();
                $count++;
            } else {
                $_md5_values[] = $md5;
            }

        }
    }

}
echo "rnrn finished removed $count duplicated images";

?>

1 REPLY

Re: failed to open stream: Permission denied in magento duplicate image removing code

We don't have the Win server to check this for sure. We suppose, the reason for the mistake is the wrong separators in the path to the file. In code you provided `/` is used but the Win server requires this `\` separator in the path to the directory. Try to use  the DS constant instead of `/`. This constant contains the correct separator for the current server that execute the script.

 

For example, replace this:

$filepath =  Mage::getBaseDir('media') .'/catalog/product' . $base_image  ;

with that:

$filepath =  Mage::getBaseDir('media') . DS . 'catalog' . DS . 'product' . $base_image  ;