Murdock Posted March 21, 2009 Share Posted March 21, 2009 Hi folks, I'm back with another silly little problem. My php skills are laughable at best so please bear with me. Here is the deal. I have a very simple piece of code that really only does two things. First it checks to see if a user has uploaded an avatar on a remote box. If they have great, let's show it. If they haven't it should instead show a default "no image" avatar. Here is everything in a nutshell: $result = @mysql_query("SELECT * FROM database WHERE blah blah blah DESC LIMIT 0,5"); if(!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } while ($row = mysql_fetch_array($result)) { $user = $row['user']; $site = "http://www.my-domain.com"; if (@getimagesize($site . "/path-to-avatars/" . $user . "_10.jpg")) { $thumb = $site . "/path-to-avatars/" . $user . "_10.jpg"; } else { $thumb = $site . "/path-to-avatars/no_image.jpg"; } echo "<li><a href=\"". $site ."/" . $user . "\"><img src=\"" . $thumb . "\"></a></li>"; } When I test this locally via WAMP it functions exactly as it should. If there is an avatar it shows and if there isn't the no_image.jpg shows instead. When I upload it the users that have an avatar continue to display correctly but those that don't instead get a "Not Found!" error and viewing the source shows why. It's still looking for the users avatar instead of switching to the no_image.jpg. Any thoughts or insight in to this quirky behavior would be greatly appreciated. Thanks for your time. Quote Link to comment https://forums.phpfreaks.com/topic/150440-solved-if-image-display-if-not-display-this-instead/ Share on other sites More sharing options...
AP81 Posted March 21, 2009 Share Posted March 21, 2009 That's strange, getimagesize is a standard PHP function, so it should be supported on PHP4 & PHP5. Just a thought, try using the file_exists function of getimagesize... Quote Link to comment https://forums.phpfreaks.com/topic/150440-solved-if-image-display-if-not-display-this-instead/#findComment-790146 Share on other sites More sharing options...
Maq Posted March 21, 2009 Share Posted March 21, 2009 To start off let's debug. Remove the @ symbols because they suppress error messages. Second, change this line to: $result = @mysql_query("SELECT * FROM database WHERE blah blah blah DESC LIMIT 0,5") or die(mysql_error()); If your SQL is failing, due to your live environment, then it will display a "descriptive" error message. Also, are you sure the "path-to-avatars" is relatively the same from your local path to the live server? Quote Link to comment https://forums.phpfreaks.com/topic/150440-solved-if-image-display-if-not-display-this-instead/#findComment-790148 Share on other sites More sharing options...
Murdock Posted March 21, 2009 Author Share Posted March 21, 2009 Thank you both for your prompt replies. Hi Maq - I went ahead and removed the @ and modified the query as suggested. After testing both locally and live everything appears to be ok on that end. Interestingly enough, I removed the @ from the getimagesize and it did indeed throw an error on my localhost. This error only displayed above each of the two users that didn't have an avatar. Warning: getimagesize(http://localhost/my-domain/path-to-avatars/trina_10.jpg) [function.getimagesize]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in C:\wamp\www\my-domain\test.php on line 23 Line 23 of course is if(getimagesize...) Oddly enough it still works locally though as expected. The users without an avatar get the no_image.jpg and the users with have the correct image displayed. The above error isn't seen when testing live however and "No Image!" still persists. Here is the entire file, not much to it: <?php $dbc = mysql_connect('localhost', 'user', 'pass'); if(!$dbc) { exit ('<p>Unable to connect to the Database Server.</p>'); } mysql_select_db('dbname', $dbc); if (!mysql_select_db('dbname')) { exit('<p>Unable to locate the database.</p>'); } $result = mysql_query("SELECT * FROM database WHERE blah blah blah DESC LIMIT 0,5") or die(mysql_error()); while ($row = mysql_fetch_array($result)) { $user = $row['user']; $site = "http://www.my-domain.com"; if (getimagesize($site . "/path-to-avatars/" . $user . "_10.jpg")) { $thumb = $site . "/path-to-avatars/" . $user . "_10.jpg"; } else { $thumb = $site . "/path-to-avatars/no_image.jpg"; } echo "<li><a href=\"". $site ."/" . $user . "\"><img src=\"" . $thumb . "\"></a></li>"; } ?> I also triple-checked the image path references to be sure and everything there appears accurate. --- Hi AP81 - I'll have to try that as well once I get back tonight. For now it's off to work. Thank you both again for your time. It's very much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/150440-solved-if-image-display-if-not-display-this-instead/#findComment-790171 Share on other sites More sharing options...
Murdock Posted March 25, 2009 Author Share Posted March 25, 2009 Hi guys, After a bit more research and generally poking around I managed to get it resolved. For some reason the server didn't like me using $site = "http://www.my-domain.com"; to locate the image files so I modified the script a wee bit to use $_SERVER['DOCUMENT_ROOT']; instead so now i'm using it like so: $user = $row['user']; $file = $_SERVER['DOCUMENT_ROOT']; $site = "http://www.my-domain.com"; if (@getimagesize($file . "/path-to-avatars/" . $user . "_10.jpg")) { $thumb = $site . "/path-to-avatars/" . $user . "_10.jpg"; } else { $thumb = $site . "/path-to-avatars/no_image.jpg"; } It's probably not the ideal solution but it works as expected. Thanks again for your help, fellas. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/150440-solved-if-image-display-if-not-display-this-instead/#findComment-793373 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.