is_dir

(PHP 4, PHP 5, PHP 7)

is_dir — Tells whether the filename is a directory

Description

boolis_dir
( string$filename
)

Tells whether the given filename is a directory.

Parameters

filename

Path to the file. If filename is a relative
filename, it will be checked relative to the current working
directory. If filename is a symbolic or hard link
then the link will be resolved and checked. If you have enabled safe mode,
or open_basedir further
restrictions may apply.

Return Values

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

Just a note for anyone who encounters is_dir() returning false on CIFS mount points or directories within those mount points on 2.6.31 and newer kernels: Apparently in new kernels they've started using the CIFS serverino option by default. With Windows shares this causes huge inode numbers and which apparently can cause is_dir() to return false. Adding the noserverino option to the CIFS mount will prevent this. This may only occur on 32 systems but I don't have a 64 bit install to test against.

Note that this functions follows symbolic links. It will return true if the file is actually a symlink that points to a directory.

An example:<phpsymlink(".", "testlink");var_dump(is_dir("testlink"));unlink("testlink");?>

Prints out:bool(true)

(Windows Note: Under recent versions of Windows you can set symlinks as long as you're administrator, but you cannot remove directory symlinks with "unlink()", you will have to use "rmdir testlink" from the shell to get rid of it.)

One note regarding checking for empty directories :>>echo (count(glob("$dir/*")) === 0) ? 'Empty' : 'Not empty';This does not work correctly on Linux.The '.' and '..' will always be returned even if no files are present in the directory.

Running PHP 5.2.0 on Apache Windows, I had a problem (likely the same one as described by others) where is_dir returned a False for directories with certain permissions even though they were accessible.

Strangely, I was able to overcome the problem with a more complete path. For example, this only displays "Works" on subdirectories with particular permissions (in this directory about 1 out of 3):

NOTE: you should obviously be checking beforehand if $dir is actually a directory, and that it is readable, as only relying on this you would assume that in both cases you have a non-empty readable directory.