Page 1 of 1
stop the 404's
Posted: Wed Aug 30, 2006 2:58 am
by php3ch0
Hi
I have a peice of code that works fine when you look at the page but shows loads of error 404 on my statistics.
The code is to show a users avitar on the chat forum. If no avitar exists then it shows the default image.
Code: Select all
$file = $host."images/avitars/".$row_img['filename'];
if (@fclose(@fopen( $file, "r")) and !empty($row_img['filename'])) {
echo $file;
} else {
echo $host."images/avitars/no_image.gif";
}
Baiscally I need to check if the file exists without showing an error 404
Posted: Wed Aug 30, 2006 3:01 am
by volka
$host is something like '
http://whatever' ?
Why does your php script try to access the files via http? Wouldn't the local filesystem do?
Btw: do you mean avatar?
Posted: Wed Aug 30, 2006 3:16 am
by php3ch0
$host is
http://whatever - it makes things easier when switching from testing server to live website:
avitar should be avatar but my spelling sucks
by using local file system do you mean using ../images/avitars/ rather than
http://www.somedomain.com/images/files ?
The reason that I do this is beacuse this is in a function that can be called anywhere on the website not just in one specific directory.
Posted: Wed Aug 30, 2006 3:21 am
by volka
You can use a config.php having
or similar.
Posted: Wed Aug 30, 2006 3:39 am
by php3ch0
thanks had a good read through
would something like this work
Code: Select all
$host = "$_SERVER['DOCUMENT_ROOT']";
$file = $host."images/avitars/".$row_img['filename'];
if (@fclose(@fopen( $file, "r")) and !empty($row_img['filename'])) {
echo $file;
} else {
echo $host."images/avitars/no_image.gif";
}
Posted: Wed Aug 30, 2006 4:01 am
by php3ch0
no thats not working. I am on a shared server and for document_root i get
server/somethingelse/username/www/
this means when I call the image I am getting
http://www.someurl.com/server/something ... es/avitars
rather than
http://www.someurl.com/images/avitars
Posted: Wed Aug 30, 2006 4:14 am
by Jenk
Don't use http:// in fopen. URL wrappers may not be enabled on your host, which is most likely.
If your images are only on your host, copy them to your dev box. Mirror the production environment.
Posted: Wed Aug 30, 2006 6:46 am
by php3ch0
I have got a mirror
the problem is on the production environment.
I need an alternative to check wheather a file exists than fopen()
This is part of a function in a config file that can be called from anywhere in the website so it can't be relative.
Thanks
Dan
Posted: Wed Aug 30, 2006 6:47 am
by volka
Do not use http and fopen. Use the local filesystem and file_exists instead.