getimagesize() and file_exists() problems

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
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

getimagesize() and file_exists() problems

Post by SBro »

I have a strange problem with some code for some images. I create the image path using both static data and a field from the database eg:

Code: Select all

$img = '/path/to/file/'.$db_field;
I then use the result of this to populate a img field, the problem being when I run a check on $img to see if it exists, it always returns false (ie. doesn't exist) even when it's clearly obvious that it does seeing as though the image displays correctly. I've tried clearing cache etc, but it doesn't seem to work. A similar problem happens with getimagesize() on $img, nothing is returned like it can't find the image, but again it does exist. I noticed if I use relative path and not absolute then things work? any reason for this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It appears you're using an absolute path. Since your web server likely isn't run off the server's file system root, that path will probably not exist. I'll guess the file exists relative to the document root of the web server. Try using $_SERVER['DOCUMENT_ROOT'] with your code.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post by SBro »

Thanks Feyd, that fixed that problem with getimagesize() etc however, if I add $_SERVER['DOCUMENT_ROOT'] to the path then when I display the image it doesn't display because it's looking for 'www.domain.com/$_SERVER['DOCUMENT_ROOT']/path/to/image' which obviously doesn't work. Is the only way around this to subtract the document root from the path when displaying the image ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

only add it when verifying that the file exists.
Post Reply