Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

File Exists not working

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sarderiftekhar
Smarty Rookie


Joined: 10 Oct 2018
Posts: 5

PostPosted: Wed Oct 10, 2018 3:02 pm    Post subject: File Exists not working Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Oct 11, 2018 11:10 am    Post subject: Reply with quote

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
View user's profile Send private message
sarderiftekhar
Smarty Rookie


Joined: 10 Oct 2018
Posts: 5

PostPosted: Thu Oct 11, 2018 11:37 am    Post subject: File Exists not working Reply with quote

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
View user's profile Send private message
sarderiftekhar
Smarty Rookie


Joined: 10 Oct 2018
Posts: 5

PostPosted: Thu Oct 11, 2018 11:39 am    Post subject: File Exists not working Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Thu Oct 11, 2018 7:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Oct 11, 2018 9:37 pm    Post subject: Re: File Exists not working Reply with quote

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
View user's profile Send private message
sarderiftekhar
Smarty Rookie


Joined: 10 Oct 2018
Posts: 5

PostPosted: Fri Oct 12, 2018 8:20 am    Post subject: File Exists not working Reply with quote

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
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Fri Oct 12, 2018 8:59 am    Post subject: Reply with quote

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
View user's profile Send private message
sarderiftekhar
Smarty Rookie


Joined: 10 Oct 2018
Posts: 5

PostPosted: Fri Oct 12, 2018 2:13 pm    Post subject: File Exists not working Reply with quote

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. Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP