PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
php3ch0
Forum Contributor
Posts: 212 Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK
Post
by php3ch0 » Wed Aug 30, 2006 2:58 am
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Aug 30, 2006 3:01 am
$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?
php3ch0
Forum Contributor
Posts: 212 Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK
Post
by php3ch0 » Wed Aug 30, 2006 3:16 am
$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.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Aug 30, 2006 3:21 am
You can use a config.php having
or similar.
php3ch0
Forum Contributor
Posts: 212 Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK
Post
by php3ch0 » Wed Aug 30, 2006 3:39 am
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";
}
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Wed Aug 30, 2006 4:14 am
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.
php3ch0
Forum Contributor
Posts: 212 Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK
Post
by php3ch0 » Wed Aug 30, 2006 6:46 am
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Aug 30, 2006 6:47 am
Do not use http and fopen. Use the local filesystem and file_exists instead.