erme Posted January 25, 2010 Share Posted January 25, 2010 Hi, Is there any way of searching for an image in a directory and saying 'if image exsists do this, else display defaullt image' Many thanks Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/ Share on other sites More sharing options...
JonnoTheDev Posted January 25, 2010 Share Posted January 25, 2010 file_exists(); <?php $filepath = "/home/test.jpg"; if(file_exists($filepath)) { } ?> Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001347 Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 if(file_exists($path)) { //use file } else { // use default } Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001348 Share on other sites More sharing options...
erme Posted January 25, 2010 Author Share Posted January 25, 2010 This is what I now have: $filepath = "http://address.co.uk/images/test/" . $r['County'] . ".gif"; echo $filepath; if (file_exists($filepath)) { echo "\n<a href=\"#\"><img src=\"/images/test/" . $r['County'] . ".gif\" /></a>\n"; } else { echo "\n<a href=\"#\"><img src=\"/images/test/default.gif\" /></a>\n"; } However the 'default.gif' always gets displayed even though 'echo $filepath;' displays the correct path (depending on what page i'm on defining County) Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001364 Share on other sites More sharing options...
erme Posted January 25, 2010 Author Share Posted January 25, 2010 OK I've managed to figure out it wasn't working for the path. Once I changed the path that the actually php was located .. it worked! Is there a way around this? Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001408 Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 The way to be certain is to always put the full url in the src attribute Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001421 Share on other sites More sharing options...
erme Posted January 25, 2010 Author Share Posted January 25, 2010 I tried that. Still didnt work. Its as though the image HAS to be in the same folder as the php. Could it be a server issue? Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001425 Share on other sites More sharing options...
jl5501 Posted January 25, 2010 Share Posted January 25, 2010 Well the issue here actually is that the path for the file_exists() call cannot be a url. It has to be a filesystem path. So dirname(__FILE__).'/images/.... would work for the check, and the url would work for the src attribute Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001444 Share on other sites More sharing options...
erme Posted January 25, 2010 Author Share Posted January 25, 2010 Right ok, how would I do somethis like this .. $path = "/images/test/" . $r['County'] . ".gif"; $filepath = dirname(__FILE__) $path; echo $filepath; Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001456 Share on other sites More sharing options...
erme Posted January 25, 2010 Author Share Posted January 25, 2010 No worries, worked it out myself: $filepath = dirname(__FILE__). "/images/test/" . $r['County'] . ".gif"; echo $filepath; Link to comment https://forums.phpfreaks.com/topic/189743-if-image-exsists/#findComment-1001470 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.