Jump to content

How to know if a picture exists or not


Clay Trainor

Recommended Posts

Hi, im very very new to php, and have only been reading up on it for a couple of days.

 

im trying to get custom pictures on my website based on how the user was directed there.  i.e.  if he clicked a link that says www.mysite.com/index.php?key=Gears%20Of%20War i use key to find "Gears of War.jpg" and display it on the page.  Alot of the links that visitors may click will not have a picture designated for them, so i want a default picture to show when "Gears of War.jpg" does not exist.

 

im assuming i need an if statement of some sort.  How exactly would i go about doing this?

 

thanks for your help

Link to comment
https://forums.phpfreaks.com/topic/42654-how-to-know-if-a-picture-exists-or-not/
Share on other sites

<?php

$img = (is_file($filepath)) ? $filepath : "images/default.gif";

?>

 

Im not entirely sure what this code means.

 

is it checking to see if "images/default.gif" exists?

Sorry, that is what's called a terneray operator - It has the same effect as an if/else statement, like so:

<?php
$filepath = "path/to/where/i/am/checking/for/images.gif";
if(is_file($filepath)) {
    $img = $filepath;
} else {
    $img = "images/default.gif";
}
?>

 

I personally would use file_exists(), but it has the same effect i think:

The only problem with using file_exists() is that it checks for both directories and files, which has the possibility of tripping you up in the future and you spending time figuring out why it's doing something you thought it shouldn't!

<?php $keyword = htmlspecialchars($_GET['key']); 

$filename = "http://www.mysite.com/pictures/tvshowdvd/<?php echo $keyword; ?>.jpg" ;


if (is_file($filename)) {
   $img = $filename ;

} else

{
   $img = "http://www.zatznotfunny.com/wordpress/wp-content/netflix.gif" ;
}

 

im using this code, from what you guys have given me.  I've tried both "is_file" and "file_exist" with no success.

 

for some reason, it always thinks that the picture exists, even when it does not, and there is just an empty picture box on my site.

 

where am i going wrong?

 

sorry for my newbieness :)

$filename = "http://www.mysite.com/pictures/tvshowdvd/<?php echo $keyword; ?>.jpg" ;

 

should be

 

$filename = "http://www.mysite.com/pictures/tvshowdvd/".$keyword.".jpg";

 

--FrosT

thanks for your help

 

unfortunately

 

same problem still persists ?

hmm tried that as well with the same results.  the value always seems to be true, even whent he picture doesnt exist....

 

my code now looks like this

 

<?php $keyword = htmlspecialchars($_GET['key']); 

$filename = '/pictures/tvshowdvd/".$keyword.".jpg';


if (is_file($filename)) {
   $img = $filename ;

} else

{
   $img = "http://www.zatznotfunny.com/wordpress/wp-content/netflix.gif" ;
}


?>

 

 

Is that the full relative path?

 

print $_SERVER['DOCUMENT_ROOT']; to verify if it is not than you need to define the whole path IE:

 

$filename = "/www/home/username/pictures/tvshowdvd/".$keyword.".jpg";

 

Note the above is NOT what you should use just a template, fill it in with the right path.

 

 

Is that the full relative path?

 

print $_SERVER['DOCUMENT_ROOT']; to verify if it is not than you need to define the whole path IE:

 

$filename = "/www/home/username/pictures/tvshowdvd/".$keyword.".jpg";

 

Note the above is NOT what you should use just a template, fill it in with the right path.

 

 

well i decided to try it with the whole path and with template idea you gave me, and no results for each.

 

i did a little research and read somewhere that file_exists only checks to see if a folder exists not a file.  the result is always coming back as false now, and i cant seem to get it to recognize any files on my server.

http://us2.php.net/manual/en/function.is-file.php

 

try looking through the user comments, they often contribute great stuff

 

 

 

yea, i was already reading through that.

 

alot of it is way over my head.  I cant beleive how difficult this fairly simple problem is to fix.  I simply just cant get the code to detect a file on my server, it's always false.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.