Page 1 of 1
if image file doesnt exist replace with a default image...
Posted: Mon Oct 24, 2005 5:22 am
by paulng
feyd | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Hello,
Wonder if any one would help , I have a directoty of images on my server and the images are referenced in the database but what Im trying to do is to check whether an image exists using the file_exist function. if the image does not exist replace with a default.
Here is my code
Code: Select all
//declaring my variables
while($row = mysql_fetch_object($result)) {
$leftblurbid = "$row->Id";
$leftblurbtitle = $row->PromotionsTitle;
$leftblurbdesc = $row->PromotionsBlurb;
$leftblurbimg = "$row->PromotionsImage";
//if condition for checking and replace the image
if ($leftblurbimg) {
$filename = "/usr/local/apache/htdocs/path/path/".$leftblurbimg.".gif";
if (!file_exists($filename)) {
$leftblurbimg = "unaivailable_news";
}
}else{
$leftblurbimg = "unaivailable_news";
Im able to check if the image is available but not able to replace with default image can anyone help..?
feyd | Please use Code: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Mon Oct 24, 2005 5:55 am
by Jenk
Hi, please use PHP tags
If you are creating a image script (that provides only the image)
Something like:
Code: Select all
<?php
$file = realpath('/path/to/images/' . $leftblurbimg . '.gif');
$default = realpath('/path/to/images/default_image.gif');
if ($file) {
$image = imagecreatefromgif($file);
} else {
$image = imagecreatefromgif($default);
}
//etc..
?>
Or, if it is part of the complete page:
Code: Select all
<?php
$file = realpath('/path/to/images/' . $leftblurbimage . '.gif');
if ($file) {
echo '<img src="relative/path/to/images/' . $leftblurbimage . '.gif" />';
} else {
echo '<img src="relative/path/to/images/default_image.gif" />';
}
//etc..
?>
HTH

Posted: Mon Oct 24, 2005 1:18 pm
by $var
another way to do it is this:
Code: Select all
<img src='<? if($sqlresults["Image"]=="none"){?>Image/Sub_Img/none.jpg<? }else{ ?><?="Imi/Sub_Img/".$sqlresults["Ven_Image"];}?>' alt="Alternate Text" width="145" height="145" border="0">
Posted: Tue Oct 25, 2005 3:25 am
by onion2k
$var wrote:Code: Select all
<img src='<? if($sqlresults["Image"]=="none"){?>Image/Sub_Img/none.jpg<? }else{ ?><?="Imi/Sub_Img/".$sqlresults["Ven_Image"];}?>' alt="Alternate Text" width="145" height="145" border="0">
You still use short PHP tags? To quote the PHP manual:
Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.
Posted: Tue Oct 25, 2005 6:32 am
by $var
k, mr. inspring...
help me out here. i hear a lot of short tag speak... but i don't mean to still use anything...
what is a non short tag version of the above code?
Posted: Tue Oct 25, 2005 7:09 am
by Grim...
Code: Select all
<img src='<?php if($sqlresults["Image"]=="none"){?>Image/Sub_Img/none.jpg<?php }else{ ?><?php echo "Imi/Sub_Img/".$sqlresults["Ven_Image"];}?>' alt="Alternate Text" width="145" height="145" border="0">
Or better still:
Code: Select all
<img src='<?php
if($sqlresults["Image"]=="none")
{
?>Image/Sub_Img/none.jpg<?php
} else {
echo "Imi/Sub_Img/".$sqlresults["Ven_Image"];
}
?>' alt="Alternate Text" width="145" height="145" border="0">
Posted: Tue Oct 25, 2005 9:06 am
by $var
okay,
so it is to my understand that popping in and out of HTML and PHP is what short tags are... correct?
using tables in HTML as opposed to echoing all structure.
i get that, i think.
why is that so bad? it makes for sloppy code? or it is harder to read in older versions?
if i am only working on one system, then what does it matter if it isn't portable?
these are not snotty questions, they are asking for comprehension only...
i'm still newbie, yes? i need to better understand this jive.

Posted: Tue Oct 25, 2005 9:10 am
by Jenk
No - the problem is short tags are not an "always on" feature of PHP, much in the same way register_globals is On for some, Off for others.
Code: Select all
?>
<img src='<?php
if($sqlresults["Image"]=="none")
{
?>Image/Sub_Img/none.jpg<?php
} else {
echo "Imi/Sub_Img/".$sqlresults["Ven_Image"];
}
?>' alt="Alternate Text" width="145" height="145" border="0">
That would work just fine, though personally I prefer to echo than to break in/out of PHP/output

Posted: Tue Oct 25, 2005 2:22 pm
by $var
but if your server is set to accept both those, and it's a server side application... then it should be fine right?
Posted: Tue Oct 25, 2005 3:07 pm
by John Cartwright
$var wrote:
so it is to my understand that popping in and out of HTML and PHP is what short tags are... correct?
Incorrect. These are short tags <? ?> these are normal tags <?php ?>
$var wrote:
but if your server is set to accept both those, and it's a server side application... then it should be fine right?
Its the whole idea of compatability.
But I mean, why use short tags when there is a chance they are off...
What if you switch to a server that has them turned off. Time to go through my hundreds of files and edit them?
of course this could be easily automated to replace them
Posted: Tue Oct 25, 2005 3:08 pm
by onion2k
$var wrote:but if your server is set to accept both those, and it's a server side application... then it should be fine right?
In my opinion .. no. It's only fine *at the moment*. If for some reason you change to a host that doesn't allow short PHP tags, or you no longer control the server, then the code would have to be modified. By using <?php ?> tags from the outset you avoid future problems.
Also, I can foresee a time when <? ?> tags are no longer supported by PHP itself .. the clash with XML tags is very annoying at times (well.. it was once

).