Jump to content

[SOLVED] If Image, Display - If Not, Display This Instead


Recommended Posts

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.

 

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?

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

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

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.