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

Php

Php Problem Overview


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 ); 		 
} 

Php Solutions


Solution 1 - Php

Both would return true on Unix systems - in Unix everything is a file, including directories. But to test if that name is taken, you should check both. There might be a regular file named 'foo', which would prevent you from creating a directory name 'foo'.

Solution 2 - Php

$dirname = $_POST["search"];
$filename = "/folder/" . $dirname . "/";

if (!file_exists($filename)) {
    mkdir("folder/" . $dirname, 0777);
    echo "The directory $dirname was successfully created.";
    exit;
} else {
    echo "The directory $dirname exists.";
}

Solution 3 - Php

I think realpath() may be the best way to validate if a path exist http://www.php.net/realpath

Here is an example function:

<?php
/**
 * Checks if a folder exist and return canonicalized absolute pathname (long version)
 * @param string $folder the path being checked.
 * @return mixed returns the canonicalized absolute pathname on success otherwise FALSE is returned
 */
function folder_exist($folder)
{
	// Get canonicalized absolute pathname
	$path = realpath($folder);

	// If it exist, check if it's a directory
	if($path !== false AND is_dir($path))
	{
		// Return canonicalized absolute pathname
		return $path;
	}

	// Path/folder does not exist
	return false;
}

Short version of the same function

<?php
/**
 * Checks if a folder exist and return canonicalized absolute pathname (sort version)
 * @param string $folder the path being checked.
 * @return mixed returns the canonicalized absolute pathname on success otherwise FALSE is returned
 */
function folder_exist($folder)
{
	// Get canonicalized absolute pathname
	$path = realpath($folder);

	// If it exist, check if it's a directory
	return ($path !== false AND is_dir($path)) ? $path : false;
}

Output examples

<?php
/** CASE 1 **/
$input = '/some/path/which/does/not/exist';
var_dump($input);				// string(31) "/some/path/which/does/not/exist"
$output = folder_exist($input);
var_dump($output);				// bool(false)

/** CASE 2 **/
$input = '/home';
var_dump($input);
$output = folder_exist($input);			// string(5) "/home"
var_dump($output);				// string(5) "/home"

/** CASE 3 **/
$input = '/home/..';
var_dump($input);				// string(8) "/home/.."
$output = folder_exist($input);
var_dump($output);				// string(1) "/"

Usage

<?php

$folder = '/foo/bar';

if(FALSE !== ($path = folder_exist($folder)))
{
	die('Folder ' . $path . ' already exist');
}

mkdir($folder);
// Continue do stuff

Solution 4 - Php

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);
}

Solution 5 - Php

I had the same doubt, but see the PHP docu:

https://www.php.net/manual/en/function.file-exists.php

https://www.php.net/manual/en/function.is-dir.php

You will see that is_dir() has both properties.

> Return Values is_dir Returns TRUE if the filename exists and is a directory, FALSE otherwise.

Solution 6 - Php

$year = date("Y");   
$month = date("m");   
$filename = "../".$year;   
$filename2 = "../".$year."/".$month;

if(file_exists($filename)){
	if(file_exists($filename2)==false){
		mkdir($filename2,0777);
	}
}else{
	mkdir($filename,0777);
}

Solution 7 - Php

$save_folder = "some/path/" . date('dmy');

if (!file_exists($save_folder)) {
   mkdir($save_folder, 0777);
}

Solution 8 - Php

This is an old, but still topical question. Just test with the is_dir() or file_exists() function for the presence of the . or .. file in the directory under test. Each directory must contain these files:

is_dir("path_to_directory/.");    

Solution 9 - Php

Well instead of checking both, you could do if(stream_resolve_include_path($folder)!==false). It is slower but kills two birds in one shot.

Another option is to simply ignore the E_WARNING, not by using @mkdir(...); (because that would simply waive all possible warnings, not just the directory already exists one), but by registering a specific error handler before doing it:

namespace com\stackoverflow;

set_error_handler(function($errno, $errm) { 
    if (strpos($errm,"exists") === false) throw new \Exception($errm); //or better: create your own FolderCreationException class
});
mkdir($folder);
/* possibly more mkdir instructions, which is when this becomes useful */
restore_error_handler();

Solution 10 - Php

This is how I do

if(is_dir("./folder/test"))
{
  echo "Exist";
}else{
  echo "Not exist";
}

Solution 11 - Php

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

Solution 12 - Php

i think this is fast solution for dir check.

$path = realpath($Newfolder);
if (!empty($path)){
   echo "1";
}else{
   echo "0";
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeterView Question on Stackoverflow
Solution 1 - PhpMarc BView Answer on Stackoverflow
Solution 2 - PhpMaherView Answer on Stackoverflow
Solution 3 - PhpinckieView Answer on Stackoverflow
Solution 4 - PhpBoolean_TypeView Answer on Stackoverflow
Solution 5 - Phpuser7906940View Answer on Stackoverflow
Solution 6 - PhpronsView Answer on Stackoverflow
Solution 7 - Phphour manView Answer on Stackoverflow
Solution 8 - PhpdjordjeView Answer on Stackoverflow
Solution 9 - PhpSebasView Answer on Stackoverflow
Solution 10 - PhpJetro BernardoView Answer on Stackoverflow
Solution 11 - PhpMáxima AlekzView Answer on Stackoverflow
Solution 12 - PhpUmut Can ArdaView Answer on Stackoverflow