[ACCEPTED]-Check whether a directory exists in PHP-file-io

Accepted answer
Score: 53

Should work correctly. From is_dir() documentation:

Returns 4 TRUE if the filename exists and is a directory, FALSE otherwise.

Well, anyway 3 if it doesn't try this:

if(file_exists($dir) && is_dir($dir))

BTW. results of these 2 functions are cached in stat cache. Use 1 clearstatcache() to clean that cache.

Score: 2

You'll probably want to use opendir() after is_dir() agrees 9 that the path (could) be a directory.

If 8 the resource returned by opendir() is valid, you 7 know you have a directory, and already have 6 a handle to read it.

Just be sure to call 5 closedir(), either way, if a valid handle 4 is returned.

Edit:

This answer assumes that 3 you'll be opening the directory either way. If 2 you just need to ensure a path is sane / valid, file_exists() is 1 much cheaper.

Score: 1
bool file_exists ( string $filename )

Checks whether a file or directory exists.

http://php.net/manual/en/function.file-exists.php

0

Score: 0

You could try this:

if(is_dir($dir) && is_writeable($dir))
{
    // ...
}

0

More Related questions