How do I check if a directory exists? "is_dir", "file_exists" or both?

How do I check if a directory exists? "is_dir", "file_exists" or both?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How do I check if a directory exists? "is_dir", "file_exists" or both?

When building file systems in PHP, creating directories is a frequent operation. A common pattern involves checking whether a target path already exists before attempting to create it to avoid throwing exceptions or encountering permission errors. Developers often debate between using is_dir() alone versus combining it with file_exists().

The standard and sufficient approach for this specific task is using is_dir(). Your initial code snippet correctly handles the logic:

if ( !is_dir( $dir ) ) {
    mkdir( $dir );       
}

This works because is_dir() explicitly checks if the path points to a directory. If it returns false, the path either does not exist or exists as a file instead of a directory. In the latter case, attempting to call mkdir() on a path that is already a regular file will trigger a warning and fail silently depending on error reporting settings.

Adding file_exists() to the condition, as in your second example:

if ( !file_exists( $dir ) && !is_dir( $dir ) ) {
    mkdir( $dir );       
} 

redundantly checks for existence twice. Since file_exists() returns true if the path exists regardless of whether it is a file or a directory, the condition !file_exists($dir) && !is_dir($dir) effectively simplifies to just !is_dir($dir) when the path does not exist, and remains false when the path is an existing directory. However, when the path is an existing regular file, file_exists() returns true, making the whole condition false, which prevents the creation attempt. While this logic avoids creating a file overwriting another file, it introduces unnecessary complexity and performance overhead by calling two system functions instead of one.

The primary pitfall to consider is not just existence, but the state of that existence. If $dir points to an existing regular file rather than a directory, is_dir() returns false. Your original code would then attempt to run mkdir(). This will fail with a E_WARNING: mkdir(): File exists error. Therefore, relying solely on is_dir() is safe provided you handle the potential failure of mkdir() gracefully or ensure that your application environment does not allow accidental file creation at that path.

In production scenarios, especially within frameworks like Laravel, it is often safer to use helper methods that abstract these checks. For instance, Laravel provides Storage::makeDirectory() which handles existence checks internally and manages permissions across different storage drivers (local, S3, etc.). If you are working outside the framework, using mkdir() with the fourth parameter set to true ensures non-recursive creation fails gracefully if intermediate directories are missing, though this does not solve the issue of an existing file at the target path.

If you need to ensure a directory exists without worrying about whether it is a file or directory (because your logic guarantees it must be a directory), is_dir() remains the correct tool. Combining checks with file_exists() adds no value in this context and obscures the intent of the code. Always prefer the simplest solution that correctly handles the edge cases relevant to your specific application logic.

Our network