0

I have developed a theme for a clients magento installation which allows manually uploaded files to be used in catalog/product/view/media.phtml. This is so I can use srcset to load higher resolution images.

The code uses file_exists() to see if the imagery has been uploaded before displaying a placeholder if nothing is present.

As soon as I turn enable the caches, the file_exists() function returns false and my placeholder is shown. If I disable the caches, the correct images return.

I have tried using relative paths as well as getcwd() and $_SERVER['DOCUMENT_ROOT'] to no avail. I have tried the same code with is_file() and is_readable() to no avail.

I have output the calculated path within the cached template to verify that the path it has generated is correct.

The only thing stopping the images from loading is enabling the caches which shaves 2 seconds off the load time so is definitely desirable.

Am I missing something or is this a bug?

Magento CE 1.9.1.0

PHP 5.4

Code:

<?php
$_product = $this->getProduct();
$_helper = $this->helper('catalog/output');
$images = $this->getGalleryImages();

$tehurl = $_product->getProductUrl();
$suburl = substr($tehurl, strrpos($tehurl, "/") + 1);
$suburl = str_replace("-", "", $suburl);
?>
<div class="product-image-gallery">
<?php if (file_exists($_SERVER['DOCUMENT_ROOT']."/assets/front/saleimg/productview/tp-1x/".$suburl."-0.png")) { ?>
    <div class="productslider">
        <div class="view">
            <ul>
<?php for ($x = 0; $x <= 2; $x++) { ?>
                <li><img src="/assets/front/saleimg/productview/tp-1x/<?php echo $suburl."-".$x; ?>.png" srcset="/assets/front/saleimg/productview/tp-2x/<?php echo $suburl."-".$x; ?>.png 2x" /></li>
<?php } ?>
            </ul>
        </div>
    </div>
<?php
}
elseif (file_exists($_SERVER['DOCUMENT_ROOT']."/assets/front/saleimg/productview/".$suburl.".jpg")) {
?>
    <img src="/assets/front/saleimg/productview/<?php echo $suburl; ?>.jpg" srcset="/assets/front/saleimg/productview/<?php echo $suburl; ?>-2x.jpg 2x" />
<?php
}
elseif (count($images) > 0) {
    $i = 0;
    foreach ($this->getGalleryImages() as $_image):
        if ($this->isGalleryImageVisible($_image)):
?>
    <img id="image-<?php echo $i; ?>" src="<?php echo $this->getGalleryImageUrl($_image); ?>" />
<?php
        endif;
        $i++;
    endforeach;
}
else {
?>
<!-- <?php
var_dump($_SERVER['DOCUMENT_ROOT']);
var_dump($_SERVER['DOCUMENT_ROOT']."/assets/front/saleimg/productview/tp-1x/".$suburl."-0.png");
$lame = $_SERVER['DOCUMENT_ROOT']."/assets/front/saleimg/productview/tp-1x/".$suburl."-0.png";
var_dump(file_exists($lame));   
var_dump(file_exists($_SERVER['DOCUMENT_ROOT']."/assets/front/saleimg/productview/tp-1x/".$suburl."-0.png")); 
var_dump(is_file($_SERVER['DOCUMENT_ROOT']."/assets/front/saleimg/productview/tp-1x/".$suburl."-0.png")); 
var_dump(is_readable($_SERVER['DOCUMENT_ROOT']."/assets/front/saleimg/productview/tp-1x/".$suburl."-0.png"));
?> -->
    <img src="/assets/front/saleimg/placeholders/productbottles.png" />
<?php } ?>
</div>
<?php echo $this->getChildHtml('after');
4
  • Can you provide the section of code doing the file_exists logic please? Ideally the whole block and related models. Commented Aug 25, 2015 at 17:26
  • added code from the media.phtml file Commented Aug 26, 2015 at 9:04
  • Have you tried using Mage::getBaseDir() to get the base magento directory? Commented Aug 26, 2015 at 9:12
  • I have tried but that makes no difference, you can see I output the $_SERVER['DOCUMENT_ROOT'] so I know the path going in to file_exists() is correct, it just returns false (edit: the code works perfectly when cache is disabled, it only stops working because of the cache) Commented Aug 26, 2015 at 13:53

2 Answers 2

0

I eventually figured out the answer, I forget where I found it now.

Magento appends a query string onto URLs generating in cache which are then filtered out before display. This means you never see them but obviously they are present when the strings are being processed.

All I had to do was add this line at the top:

$tehurl = substr($_product->getProductUrl(), 0, (strpos($_product->getProductUrl(), "?")) ?: strlen($_product->getProductUrl()));
0

In my case it was a file ownership and permission issue.

Change the owner:group of the file to web-server-user-name nginx)

#chown -R web-server-user-name .

#i.e if running nginx 
chown -R nginx .

If it does not solved the issue, change all file/folder permission

chmod -R 775 .

For more about file permission: Magento: Recommended File System Ownership and Privileges

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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