Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck if file exists before displaying in Magento PHP?
    primarykey
    data
    text
    <p>I am able to get the web path to the file like so:</p> <pre><code>$filename = 'elephant.jpg'; $path_to_file = $this-&gt;getSkinUrl('manufacturertab'); $full_path = $path_to_file . '/' . $filename; </code></pre> <p>But if the file doesn't exist, then I end up with a broken image link.</p> <p>I tried this:</p> <pre><code>if(!file_exists($full_path)) { Mage::log('File doesn\'t exist.'); } else { ?&gt;&lt;img src="&lt;?php echo $full_path ?&gt;" /&gt;&lt;?php } </code></pre> <p>Of course that didn't work because <code>file_exists</code> does not work on urls.</p> <p>How do I solve this?</p> <p>1.) Can I translate between system paths and web urls in Magento?</p> <p>e.g. something like (pseudocode):</p> <pre><code>$system_path = $this-&gt;getSystemPath('manufacturertab'); </code></pre> <p>That looks symmetrical and portable.</p> <p>or 2.) Is there some PHP or Magento function for checking remote resource existence? But that seems a waste, since the resource is really local. It would be stupid for PHP to use an http method to check a local file, wouldn't it be?</p> <p>Solution I am currently using:</p> <pre><code>$system_path = Mage::getBaseDir('skin') . '/frontend/default/mytheme/manufacturertab'; // portable, but not pretty $file_path = $system_path . '/' . $filename; </code></pre> <p>I then check if <code>file_exists</code> and if it does, I display the <code>img</code>. But I don't like the asymmetry between having to hard-code part of the path for the system path, and using a method for the url path. It would be nice to have a method for both.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload