PHP is_dir() Function
Quick summary: The PHP is_dir() function checks whether a path is a directory.
PHP is_dir() Syntax
is_dir(string $filename): bool
PHP
PHP is_dir() Basic examples
var_dump(is_dir('logs'));
PHP
Output:
bool(true)
Returns true for directories.
PHP is_dir() Real-world usage
if (!is_dir('uploads')) {
mkdir('uploads');
}
PHP
Output:
bool(true)
Ensures a directory exists.
PHP is_dir() Edge cases
var_dump(is_dir('config.php'));
PHP
Output:
bool(false)
Files are not directories.
PHP is_dir() Common mistakes
Assuming is_dir checks existence only
is_dir checks type, not just existence.
Incorrect
is_dir('path');
Correct
file_exists('path');
Use file_exists for general existence checks.
PHP is_dir() Frequently Asked Questions
What does is_dir() do?
Checks whether a path is a directory.
Difference from is_file()?
is_dir() checks directories only.
Use case?
Validating folder paths.
Returns false for files?
Yes.
Works with symlinks?
Yes.
Performance?
Fast.
Common mistake?
Using instead of file_exists().
Safe?
Yes.
Can fail?
Yes due to permissions.
Alternative?
file_exists().
Cache issues?
Yes, use clearstatcache().
Best practice?
Combine with is_readable().