stop the 404's

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

Post Reply
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

stop the 404's

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You can use a config.php having

Code: Select all

$basedir = dirname(_FILE_);
or similar.
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post 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"; 
                 }
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
php3ch0
Forum Contributor
Posts: 212
Joined: Sun Nov 13, 2005 7:35 am
Location: Folkestone, Kent, UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Do not use http and fopen. Use the local filesystem and file_exists instead.
Post Reply