file_exists problem

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
bluesman333
Forum Commoner
Posts: 52
Joined: Wed Dec 31, 2003 9:47 am

file_exists problem

Post by bluesman333 »

I'm trying to use the file_exists function to check that an image exists before displaying a link.

Code: Select all

function image_exists($image_name) {
  
   $dir = "http://domain.com/images/";
 
     if(file_exists($dir.$image_name) {
          return true;
     }else{
          return false;
     }

}

It's always returning false - even if the image exists.


Any suggestions?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I'm pretty sure you can't use URL wrappers with file_exists(). Are you trying to check if an image exists on another server?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

As pickle said, you can't use file_exists doesn't have the wrappers.

Try something like:

Code: Select all

function image_exists($image_name) {
	$dir = "http://domain.com/images/";
	$f = fopen($dir.$image_name, "r");
	if(!$f) return false;
	fclose($f);
	return true;
}
Post Reply