file_exists vs. is_file/is_dir

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
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

file_exists vs. is_file/is_dir

Post by oboedrew »

There are several instances where I need to check to see if a certain file or directory exists. I do not need to determine if the item in question is a file or directory. I know which it will be, if it exists. I just need to check to see if it exists. In such cases, is it best to use is_file for files and is_dir for directories, or should I just use file_exists for both? Is there any advantage to one over the other? Is one faster?

Thanks,
Drew
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: file_exists vs. is_file/is_dir

Post by greyhoundcode »

If you only need to check that it exists then just use file_exists(), the difference between that (as I understand it) and is_file() or is_dir() is that it doesn't differentiate between files and directories. Might save you a couple of lines of code.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: file_exists vs. is_file/is_dir

Post by oboedrew »

I don't see how it would save me lines of code. It's the difference between...

if(file_exists($whatever)){ do this }

and either...

if(is_file($whatever)){ do this }

or...

if(is_dir($whatever)){ do this }

I'm just wondering if either function is known to be faster and more efficient, or if either is considered better style, given that I only need to see if the file/directory exists, not to determine whether it is a file or directory.

Cheers,
Drew
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: file_exists vs. is_file/is_dir

Post by semlar »

How would it not save you lines of code?

You said you need to check if something, which is either a file or a directory, exists.

You're either using file_exists, or both is_dir and is_file. You can't check if a file exists with is_dir..

Regardless, is_dir and is_file are for checking if the path exists and checking if it's either a directory or a file. I don't see how they could be more efficient or faster than just checking if the path exists.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: file_exists vs. is_file/is_dir

Post by oboedrew »

semlar wrote:You're either using file_exists, or both is_dir and is_file. You can't check if a file exists with is_dir..
No, I'm either using file_exists, or either is_dir or is_file. Not both. There is no need for both. If the file/directory in question exists, it will exist only because it was created by another script of mine. I will know whether it is a file or a directory. There is no need to determine which. I only need to check to see if it exists.
semlar wrote:Regardless, is_dir and is_file are for checking if the path exists and checking if it's either a directory or a file. I don't see how they could be more efficient or faster than just checking if the path exists.
I am aware that is_dir and is_file check to see if a path exists and also determine whether or not it is a file/dir. I am asking if anyone knows of a good reason to use is_file or is_dir over file_exists, given that I only need to check for the existence of a file or directory, not to determine whether or not something is a file or directory. For examle, is one of these functions known to be faster?

Thanks,
Drew
Post Reply