ufbobbo Posted March 6, 2010 Share Posted March 6, 2010 Hi there, I just started using PHP tonight and I'm trying to figure out a basic if then else statement using file_exists to check if an image file exists on the server. If it exists, I'd like to display it otherwise I would like to display a generic image. Any help as to why I'm getting errors in the code? <?php if (file_exists("../../<?php $key="applogo"; echo get_post_meta($post->ID, $key, true); ?>.png")) { ?> <img src="../../wp-content/gallery/app_logos/genericlogo.png" width="50" height="50"> <?php } else { ?> <img src="../../wp-content/gallery/app_logos/<?php $key="applogo"; echo get_post_meta($post->ID, $key, true); ?>.png" width="50" height="50"> <?php endif; ?> Thank you kindly for any help you could give! Link to comment https://forums.phpfreaks.com/topic/194309-basic-php-help-ultimate-beginner/ Share on other sites More sharing options...
teamatomic Posted March 6, 2010 Share Posted March 6, 2010 Dont break PHP, echo the img line, dont use endif, not needed with the use of brackets. <?php if(file_exists("$file")) { echo '<img src'$img_file">'; } else { echo '<img src="$img_file_2">'; } ?> HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/194309-basic-php-help-ultimate-beginner/#findComment-1022204 Share on other sites More sharing options...
ufbobbo Posted March 6, 2010 Author Share Posted March 6, 2010 Teamatomic, Thank you for your help, but I am still getting an error when I try to implement it this way (sorry, I am very new to programming so thank you for your patience): <?php $key="applogo" $appname=get_post_meta($post->ID, $key, true); echo $appname; $appfilename="http://www.websitename.com/wp-content/gallery/app_logos/$appname"; echo $appfilename; if(file_exists("$appfilename")) { echo '<img src="http://www.websitename.com/wp-content/gallery/app_logos/$appname" width="100" height="100">'; } else { echo '<img src="http://www.websitename.com/wp-content/gallery/app_logos/genericlogo.png" width="100" height="100">'; } ?> Parse error: syntax error, unexpected T_VARIABLE Link to comment https://forums.phpfreaks.com/topic/194309-basic-php-help-ultimate-beginner/#findComment-1022205 Share on other sites More sharing options...
ufbobbo Posted March 6, 2010 Author Share Posted March 6, 2010 Ahhh.. finally figured it out (I'm slow ). Thank you for the guidance! <?php $key="applogo"; $appname=get_post_meta($post->ID, $key, true); $appfilename="actual_server_path_NOT_website_path/$appname.png"; if(file_exists("$appfilename")) { echo '<img src="http://www.websitename.com/wp-content/gallery/app_logos/'.$appname.'.png" width="100" height="100">'; } else { echo '<img src="http://www.websitename.com/wp-content/gallery/app_logos/genericlogo.png" width="100" height="100">'; } ?> Link to comment https://forums.phpfreaks.com/topic/194309-basic-php-help-ultimate-beginner/#findComment-1022208 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.