Clay Trainor Posted March 14, 2007 Share Posted March 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
zq29 Posted March 14, 2007 Share Posted March 14, 2007 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"; ?> Quote Link to comment Share on other sites More sharing options...
Clay Trainor Posted March 14, 2007 Author Share Posted March 14, 2007 <?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? Quote Link to comment Share on other sites More sharing options...
papaface Posted March 14, 2007 Share Posted March 14, 2007 I personally would use file_exists(), but it has the same effect i think: if (file_exists($somefilename)) { echo "exists"; } else { echo "doesnt exist"; } Quote Link to comment Share on other sites More sharing options...
zq29 Posted March 14, 2007 Share Posted March 14, 2007 <?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! Quote Link to comment Share on other sites More sharing options...
Clay Trainor Posted March 14, 2007 Author Share Posted March 14, 2007 <?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 Quote Link to comment Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 $filename = "http://www.mysite.com/pictures/tvshowdvd/<?php echo $keyword; ?>.jpg" ; should be $filename = "http://www.mysite.com/pictures/tvshowdvd/".$keyword.".jpg"; --FrosT Quote Link to comment Share on other sites More sharing options...
Clay Trainor Posted March 14, 2007 Author Share Posted March 14, 2007 $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 ? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 Well, another reasoning is I think is_file depends on the relative path, not the virtual path. http://us3.php.net/manual/en/function.is-file.php basically it needs to be $filename = '/yourpath/to/pictures/image.jpg'; --FrosT Quote Link to comment Share on other sites More sharing options...
Clay Trainor Posted March 14, 2007 Author Share Posted March 14, 2007 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" ; } ?> Quote Link to comment Share on other sites More sharing options...
Clay Trainor Posted March 14, 2007 Author Share Posted March 14, 2007 i really wasnt expecting this to be such a difficult task ??? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 '/pictures/tvshowdvd/".$keyword.".jpg'; wrong "/pictures/tvshowdvd/".$keyword.".jpg"; right Quote Link to comment Share on other sites More sharing options...
Clay Trainor Posted March 14, 2007 Author Share Posted March 14, 2007 '/pictures/tvshowdvd/".$keyword.".jpg'; wrong "/pictures/tvshowdvd/".$keyword.".jpg"; right thanks again man same crap is still happening though im really stumped here. I've been playing with this all day, with no success Quote Link to comment Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 14, 2007 Share Posted March 14, 2007 $filename = '/pictures/tvshowdvd/".$keyword.".jpg'; should be $filename = "/pictures/tvshowdvd/". $keyword. ".jpg"; Quote Link to comment Share on other sites More sharing options...
Clay Trainor Posted March 14, 2007 Author Share Posted March 14, 2007 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. Quote Link to comment Share on other sites More sharing options...
per1os Posted March 15, 2007 Share Posted March 15, 2007 http://us2.php.net/manual/en/function.is-file.php try looking through the user comments, they often contribute great stuff Quote Link to comment Share on other sites More sharing options...
Clay Trainor Posted March 15, 2007 Author Share Posted March 15, 2007 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. 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.