Question: I want to create a directory if it does not exist already.

Is using the `is_dir` function enough for that purpose?

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

```

Or should I combine `is_dir` with `file_exists`?

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

```

Answer: 
Second variant in question post is not ok, because, if you already have file with the same name, but it is not a directory, `!file_exists($dir)` will return `false`, folder will not be created, so error `"failed to open stream: No such file or directory"` will be occured. In Windows there is a difference between 'file' and 'folder' types, so need to use `file_exists()` and `is_dir()` at the same time, for ex.:

```
if (file_exists('file')) {
    if (!is_dir('file')) { //if file is already present, but it's not a dir
        //do something with file - delete, rename, etc.
        unlink('file'); //for example
        mkdir('file', NEEDED_ACCESS_LEVEL);
    }
} else { //no file exists with this name
    mkdir('file', NEEDED_ACCESS_LEVEL);
}

```
A way to check if a path is directory can be following:

```
function isDirectory($path) {
    $all = @scandir($path);
    return $all !== false;
}

```

> 
> NOTE: It will return false for non-existant path too, but works perfectly for UNIX/Windows
> 
> 
>