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

If File Exists

 
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
jcx1028
Smarty Rookie


Joined: 13 Apr 2012
Posts: 12

PostPosted: Fri Apr 13, 2012 6:44 pm    Post subject: If File Exists Reply with quote

Hi there. I've been working with Flynax's auto classifieds script, which is the first I've ever seen or used Smarty. And honestly, I'm not the best with regular PHP to begin with. So please pardon me while I ask a potentially dumb question.

On the dealer profile pages of our site (http://www.theusedcarplace.com/index.php?page=dealer-accounts&id=24), I'm using the following code to pull in a JPG of the dealer's most recent print ads.

Code:
<div class="print-ads">
<a href="http://www.theusedcarplace.com/print-ads/{$account.Full_name|replace:" ":"_"|lower}.pdf" target="_blank"><img src="http://www.theusedcarplace.com/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
<p>Click to View PDF</p>
</a>
</div>


The problem is, on pages for dealers who haven't run a print ad (http://www.theusedcarplace.com/index.php?page=dealer-accounts&id=4), there obviously isn't a corresponding JPG, so I get a missing image.

If possible, I'd like to use an {if} statement to show the JPG if it exists, but to show nothing if it does not. Here's what I've been playing with...

Code:
{assign var="printad" value="http://www.theusedcarplace.com/print_ads/{$account.Full_name|replace:" ":"_"|lower}.jpg"}
{if file_exists($printad)}
<div class="print-ads">
<a href="http://www.theusedcarplace.com/print-ads/{$account.Full_name|replace:" ":"_"|lower}.pdf" target="_blank"><img src="http://www.theusedcarplace.com/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
<p>Click to View PDF</p>
</a>
</div>
{/if}


...Except that doesn't work. It pretty much breaks the entire site.

Any suggestions?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Apr 13, 2012 7:19 pm    Post subject: Reply with quote

Is the image file a local file, or on a remote machine? You cannot use file_exists() on a URL, but you can use it on a local file. I'd try using a local file path instead of a URL, if possible.


Code:
{if file_exists('../path/to/file.jpg')}
...
{/if}


Of course the BEST solution is to handle all this stuff on the PHP side before you touch a template, and assign a flag and/or corrected filepath, so you just do something like this:

Code:
$ad['exists'] = file_exists($path_to_image);
if($ad['exists']) {
  $ad['url_pdf'] = $url_to_pdf;
  $ad['url_image'] = $url_to_image;
}
$smarty->assign('ad',$ad);


Code:
{if $ad.exists}
  <a href="{$ad.url_pdf}">{$ad.url_image}</a>
{/if}



[note] breaks entire site because php error on file_exists(URL), see your php error log.
Back to top
View user's profile Send private message Visit poster's website
jcx1028
Smarty Rookie


Joined: 13 Apr 2012
Posts: 12

PostPosted: Tue Apr 17, 2012 5:30 pm    Post subject: Reply with quote

I've got the path changed to a local file rather than a URL, but it's still not really working. It's showing ".pdf'} 0" in the spot where I'd like it to display the JPG. Here's what I'm using at the moment...

Code:
{assign var="printad" value='/print-ads/{$account.Full_name|replace:" ":"_"|lower}.pdf'}
{if file_exists($printad)}
<img src="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
{else}
0
{/if}


Could it be because of the nested brackets in the first line? Because while the following code doesn't work either, it merely shows a "0"...

Code:
{assign var="printad" value='/print-ads/[$account.Full_name|replace:" ":"_"|lower].pdf'}
{if file_exists($printad)}
<img src="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
{else}
0
{/if}
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Apr 17, 2012 5:45 pm    Post subject: Reply with quote

Nesting of tags does work only in double quoted strings.

Code:
{assign var="printad" value="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.pdf"}
Back to top
View user's profile Send private message
jcx1028
Smarty Rookie


Joined: 13 Apr 2012
Posts: 12

PostPosted: Tue Apr 17, 2012 5:50 pm    Post subject: Reply with quote

Using the following (with the double quotes) breaks both the Dealer Details page (http://www.theusedcarplace.com/index.php?page=dealer-accounts&id=44), where the JPG will ideally pop up and the full list of Dealers (http://www.theusedcarplace.com/index.php?page=dealer-accounts)...

Code:
{assign var="printad" value="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.pdf"}
{if file_exists($printad)}
<img src="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
{else}
0
{/if}
Back to top
View user's profile Send private message
jcx1028
Smarty Rookie


Joined: 13 Apr 2012
Posts: 12

PostPosted: Wed Apr 18, 2012 3:28 pm    Post subject: Reply with quote

Through trial and error (lots and lots of error), I finally figured it out. Here's what eventually worked...

Code:
{assign var="img_path" value=$account.Full_name|replace:" ":"_"|lower}
{assign var="image"
value="../public_html/print-ads/$img_path.jpg"}
{if file_exists($image)}
<div class="print-ads">
<a href="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.pdf" target="_blank"><img src="/print-ads/{$account.Full_name|replace:" ":"_"|lower}.jpg" />
<p>Click to View PDF</p>
</a>
</div>
{/if}


If you'll excuse me, I now need to go run a victory lap around the building. Probably shirtless. Because why the hell not, right?
Back to top
View user's profile Send private message
jcx1028
Smarty Rookie


Joined: 13 Apr 2012
Posts: 12

PostPosted: Wed Apr 18, 2012 3:28 pm    Post subject: Reply with quote

Thank you all for your help, by the way. I appreciate it.
Back to top
View user's profile Send private message
Watchopolis
Smarty Rookie


Joined: 06 Aug 2014
Posts: 5

PostPosted: Thu Nov 20, 2014 12:04 am    Post subject: Reply with quote

can anyone help me

my template is like this

Code:

{if $logo.exist}
    {$logo.url_image}
 {else}
     {$mov.title}
{/if}


what i try to do is i want to show {$mov.title} on the page when the {$logo.url_image} does not exist on my local file/folder, but if {$logo.url_image} does exist i want to hidde the {$mov.title} from page


can anyone help me i am new in php and smarty
i hope you understand me

Thanks you very much.


EDIT: ok i made and it works like this

Code:
{if $logo.url_image}
        <img src="{$logo.url_image}">   
        {else}
        {$mov.title}
        {/if}


the above code works then what is this {$logo.exist} for ?
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