View previous topic :: View next topic |
Author |
Message |
sarderiftekhar Smarty Rookie
Joined: 10 Oct 2018 Posts: 5
|
Posted: Wed Oct 10, 2018 3:02 pm Post subject: File Exists not working |
|
|
HI, I am trying to do this. But its always going to the else condition even though the file exists.
{assign var="image" value="{$asset}/og-image.png"}
<h1>{$image}</h1>
{if ($image)}
<img src="{$asset}/og-image1.png" alt="">
{else}
<img src="{URL::to('/')}/src/img/facebook/{$product}/facebook.jpg"" alt="">
{/if}
[img]http://prntscr.com/l4hi2h[/img]
http://prntscr.com/l4hi2h |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Oct 11, 2018 11:10 am Post subject: |
|
|
I don't see any "file_exists" in your code.
But given the code provided, it should never work.
file_exists works with real filesystem paths, not URL paths.
Provide an actual FS path and it will work. |
|
Back to top |
|
sarderiftekhar Smarty Rookie
Joined: 10 Oct 2018 Posts: 5
|
Posted: Thu Oct 11, 2018 11:37 am Post subject: File Exists not working |
|
|
HI Sorry , put on the wrong code.
{assign var="image" value="{$asset}/og-image.png"}
<h1>{$image}</h1>
{if file_exists($image)}
<img src="{$asset}/og-image.png" alt="">
{else}
<img src="{URL::to('/')}/src/img/facebook/{$product}/facebook.jpg"" alt="">
{/if} |
|
Back to top |
|
sarderiftekhar Smarty Rookie
Joined: 10 Oct 2018 Posts: 5
|
Posted: Thu Oct 11, 2018 11:39 am Post subject: File Exists not working |
|
|
I even tried like this
{if file_exists("{$asset}/og-image.png")}
<img src="{$asset}/og-image.png" alt="">
{else}
<img src="{URL::to('/')}/src/img/facebook/{$product}/facebook.jpg"" alt="">
{/if} |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Thu Oct 11, 2018 7:56 pm Post subject: |
|
|
I'm going to guess that $asset is a string that, for the purposes of the <img> tag, is a path relative to the web site's doc_root (public_html).
As such, this will be different than what the file_system path needs to be to reach the file. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Oct 11, 2018 9:37 pm Post subject: Re: File Exists not working |
|
|
sarderiftekhar wrote: | I even tried like this |
Please do read what I wrote.
Both your codes suffer from same issue - they provide an URL path, not real filesystem path. |
|
Back to top |
|
sarderiftekhar Smarty Rookie
Joined: 10 Oct 2018 Posts: 5
|
Posted: Fri Oct 12, 2018 8:20 am Post subject: File Exists not working |
|
|
HI
The $asset is a variable that changes every time. So can't put a fixed address.
The scenario is there are lot of websites sharing a single base template. And I need to set query that if Image1 is not provided then display Image2.
Image1 is a preview of that page. If that is not created then default generic preview will display. Which is Image2
Now this $asset variable is holding the address of the images folder of the websites or the pages. So its not fixed. Image2 address is fixed.
Now please can anyone give a clue how can I check this with smarty? as its designed with smarty. |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Fri Oct 12, 2018 8:59 am Post subject: |
|
|
Do you have any editing capability to the PHP code?
If so, then where $asset is assigned:
Code: | $smarty->assign('asset',whatever_function()); | could be a good place to check if the image exists.
You might also assign to a template variable the server's real path to your site, then $asset would be the rest of the path to the image folder.
Code: | {if file_exists("`$sitepath$asset`/og-image.png")} |
Even if not having access to the PHP code, there is available:
$smarty.server.DOCUMENT_ROOT
which could give you the same as $sitepath above:
Code: | {if file_exists("`$smarty.server.DOCUMENT_ROOT`/`$asset`/og-image.png")} |
|
|
Back to top |
|
sarderiftekhar Smarty Rookie
Joined: 10 Oct 2018 Posts: 5
|
Posted: Fri Oct 12, 2018 2:13 pm Post subject: File Exists not working |
|
|
Thanks bsmither,
which could give you the same as $sitepath above:
Code:
{if file_exists("`$smarty.server.DOCUMENT_ROOT`/`$asset`/og-image.png")}
This gave the idea, and I solved it. |
|
Back to top |
|
|