fubarur Posted March 6, 2007 Share Posted March 6, 2007 I need help on a bit of code to look in a folder and see if there is a image with a certain name, if there is not one display this image instead. The details looking into this folder /images/event/XX/images/ XX is $id so it is a variable depending on the event you are looking at. The image name header_1.jpg else if it can not find the above load this default image /images/event_header.jpg Any help or pointers would be appreciated. I'm sure this is a noob type question, but I'm learning and this is one great resource! Quote Link to comment Share on other sites More sharing options...
willpower Posted March 6, 2007 Share Posted March 6, 2007 if (file_exists("http://whatever")) { //do this } else { //do that } Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 6, 2007 Share Posted March 6, 2007 you need file_exists() and you may get some use out of mime_content_type() @willpower: i believe you should only use the path for file_exists. not the url. Quote Link to comment Share on other sites More sharing options...
willpower Posted March 6, 2007 Share Posted March 6, 2007 that may be the case, however i have used this on external domains and it has worked. Quote Link to comment Share on other sites More sharing options...
fubarur Posted March 6, 2007 Author Share Posted March 6, 2007 if (file_exists("http://whatever")) { //do this } else { //do that } I tried this and it did not work? <?php if (file_exists("<?=Environment::webroot()?>/images/event/$id/images/header_1.jpg")) { <img border="0" src="<?=Environment::webroot()?>/images/event/$id/images/header_1.jpg>" /> } else { <img border="0" src="<?=Environment::webroot()?>/images/header_1.jpg>" /> } ?> Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted March 6, 2007 Share Posted March 6, 2007 @fubarur: you've got the syntax wrong. try: if (file_exists("/images/event/$id/images/header_1.jpg")) @willpower: good to know =) Quote Link to comment Share on other sites More sharing options...
willpower Posted March 6, 2007 Share Posted March 6, 2007 hmmm not sure about the code thing going on there...BUT you have not escaped your php if (file_exists("<?=Environment::webroot()?>/images/event/$id/images/header_1.jpg")) {?> <img border="0" src="<?=Environment::webroot()?>/images/event/$id/images/header_1.jpg>" /> <?} else {?> <img border="0" src="<?=Environment::webroot()?>/images/header_1.jpg>" /> <?} ?> Quote Link to comment Share on other sites More sharing options...
fubarur Posted March 6, 2007 Author Share Posted March 6, 2007 @fubarur: you've got the syntax wrong. try: if (file_exists("/images/event/$id/images/header_1.jpg")) @willpower: good to know =) This gave me a blank page like the last one did <?php if (file_exists("/images/event/$id/images/header_1.jpg")) { <img border="0" src="<?=Environment::webroot()?>/images/event/$id/images/header_1.jpg>" /> } else { <img border="0" src="<?=Environment::webroot()?>/images/header_1.jpg>" /> } ?> With the above code removed page loads fine Quote Link to comment Share on other sites More sharing options...
willpower Posted March 6, 2007 Share Posted March 6, 2007 have you escaped your php as per my post. no evidence of it on yoru update Will Quote Link to comment Share on other sites More sharing options...
fubarur Posted March 7, 2007 Author Share Posted March 7, 2007 have you escaped your php as per my post. no evidence of it on yoru update Will What do you mean "Escaped your PHP"? Quote Link to comment Share on other sites More sharing options...
willpower Posted March 7, 2007 Share Posted March 7, 2007 read my post! i gave you code and you dont seem to have used it. You have PHP and HTML mixed together. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 7, 2007 Share Posted March 7, 2007 <?php if (file_exists("/images/event/$id/images/header_1.jpg")) { echo"<img border='0' src='<?php Environment: :webroot()?>/images/event/$id/images/header_1.jpg'></img>>"; }else{ echo"<img border='0' src='<?php Environment: :webroot()?>/images/header_1.jpg'></img>"; } ?> Quote Link to comment Share on other sites More sharing options...
fubarur Posted March 7, 2007 Author Share Posted March 7, 2007 read my post! i gave you code and you dont seem to have used it. You have PHP and HTML mixed together. Ok, I think this is what you are talking about, but I used this code below and get a blank page? <?php if (file_exists("/images/events/$id/images/header_1.jpg")) ?> <img border="0" src="<?=Environment::webroot()?>/images/events/$id/images/header_1.jpg"> <?php else; ?> <img border="0" src="<?=Environment::webroot()?>/images/header.jpg"> <?php if (file_exists("/images/event/$id/images/header_1.jpg")) { echo"<img border='0' src='<?php Environment: :webroot()?>/images/event/$id/images/header_1.jpg'></img>>"; }else{ echo"<img border='0' src='<?php Environment: :webroot()?>/images/header_1.jpg'></img>"; } ?> Your code <?php if (file_exists("/images/events/$id/images/header_1.jpg")) { echo"<img border='0' src='/images/events/$id/images/header_1.jpg'></img>>"; }else{ echo"<img border='0' src='images/header.jpg'></img>"; } ?> Gives me a page but its not finding the image, and its there? Quote Link to comment Share on other sites More sharing options...
fubarur Posted March 7, 2007 Author Share Posted March 7, 2007 Good news! After looking at all your help I messed around with the code and got it to work! if (file_exists("/var/www/html/images/events/$id/images/header_1.jpg")) { echo"<img border='0' src='/images/events/$id/images/header_1.jpg'></img>"; }else{ echo"<img border='0' src='images/header.jpg'></img>"; } ?> Thanks for all your help and guidance! 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.