Magento-1.9 – file_exists, is_file, and is_readable Not Working with Cache Enabled

cachemagento-1.9

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');

Best Answer

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()));
Related Topic