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
Share on other sites

I simple example would be to do the following, you'll need to do a bit moer work on it to make it suitable for production use, but that's the basic theory.

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

Link to comment
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!

Link to comment
Share on other sites

<?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 :)

Link to comment
Share on other sites

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


?>

 

 

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.