TKO Posted November 13, 2012 Share Posted November 13, 2012 Hello i am tring to show $image1 if it exists and if not show $image2 This script shows $image1 but if i erase the image it just shows a broken image icon and doesnt go to $image2 any help in this would be great <?php $image0 = $_SESSION['ls_user']; $image1 = "<img src=img/$image0.png>"; $image2 = "<img src=img/default.png>"; if ( $image1 ) { echo $image1; } else { echo $image2; } ?> thank you Quote Link to comment Share on other sites More sharing options...
Jessica Posted November 13, 2012 Share Posted November 13, 2012 That doesn't check if the image actually exists. You just set $image1, it will always evaluate to true. If you want to check for the file with that name, use file_exists Quote Link to comment Share on other sites More sharing options...
TKO Posted November 13, 2012 Author Share Posted November 13, 2012 thanks for the reply now the code only shows $image2 <?php $image0 = $_SESSION['ls_user']; $image1 = "<img src=img/$image0.png>"; $image2 = "<img src=img/default.png>"; if (file_exists( $image1 )) { echo $image1; } else { echo $image2; } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 13, 2012 Share Posted November 13, 2012 You're using it wrong. Read the manual and give it another shot: file_exists Quote Link to comment Share on other sites More sharing options...
TKO Posted November 14, 2012 Author Share Posted November 14, 2012 hmm i dont see it ?? when i test echo $image1 it shows. the only thing i can think of is echo "$image1"; } else { echo "$image2"; which still shows $image2 Quote Link to comment Share on other sites More sharing options...
codefossa Posted November 14, 2012 Share Posted November 14, 2012 You need to use an actual path to the image. Also, to check if it exists and that it's an image all in one, you can do if (getimagesize($fn)) and it works well. (: It may be possible that some image filetypes ain't supported by it though, but all the common ones are. Also, you should put {} around your variables when in a string, but you don't need to use the quotes there so it would make more sense to just echo the var. PS: $fn or filename would be the path to it such as /var/www/images/mypic.jpg or if you're in /var/www already you could just do ./images/mypic.jpg but you don't use an HTML image tag. Quote Link to comment Share on other sites More sharing options...
winningdave Posted November 14, 2012 Share Posted November 14, 2012 $image0 = $_SESSION['ls_user']; $image = "img/".$image0.".png"; $image1 = "<img src=img/$image0.png>"; $image2 = "<img src=img/default.png>"; if (file_exists( $image )) { echo $image1; } else { echo $image2; } Quote Link to comment Share on other sites More sharing options...
TKO Posted November 14, 2012 Author Share Posted November 14, 2012 wow thanks for the reply's i will try them when i get home. Thank you Quote Link to comment Share on other sites More sharing options...
TKO Posted November 15, 2012 Author Share Posted November 15, 2012 Thank you all for your reply's winningdave solution worked perfect. Thank you winningdave Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.