if image file doesnt exist replace with a default image...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulng
Forum Commoner
Posts: 28
Joined: Mon Jul 11, 2005 9:31 am

if image file doesnt exist replace with a default image...

Post by paulng »

feyd | Please use

Code: Select all

and

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

and

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]
Last edited by paulng on Mon Nov 07, 2005 4:32 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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">
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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">
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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. :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
User avatar
$var
Forum Contributor
Posts: 317
Joined: Thu Aug 18, 2005 8:30 pm
Location: Toronto

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
Last edited by John Cartwright on Tue Oct 25, 2005 3:08 pm, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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 :) ).
Post Reply