Jump to content

[SOLVED] help to Find an image and if there is not one show this one


fubarur

Recommended Posts

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!

Link to comment
Share on other sites

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>" /> 
}


?>	



Link to comment
Share on other sites

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>" />

<?}

 

 

?>

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

<?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>";
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.