we want to create a directory if it does not exist. Is just using the is_dir function ok for that purpose? For example:

php code
if (!is_dir($dir)) 
{
    mkdir($dir);         
}
Or should I also use file_exists? For example:

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

  • 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’.

javascript code
$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.";
}
[ad type=”banner”]

  • In this scenario, we want to create a folder called “images”:
php code
<?php

//The name of the directory that we need to create.
$directoryName = 'images';

//Check if the directory already exists.
if(!is_dir($directoryName)){
    //Directory does not exist, so lets create it.
    mkdir($directoryName, 0755);
}

An explanation of the PHP code snippet above:

  • We set the name of the directory that we want to create. In this case, it is “images”.
  • We used PHP’s is_dir function to check if the folder already exists. If the is_dir function returns a boolean FALSE value, then we know that the directory in question doesn’t already exist.
  • If the directory doesn’t already exist, we can create it using the mkdir function.

Note :that if you fail to check for the directory’s existence, you run the risk of getting a nasty PHP warning:

  • 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.:
javascript code
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);
}
[ad type=”banner”]

Sample code:

javascript code
$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);
}

  • 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:
javascript code
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();

 

Categorized in: