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

[pastacode lang=”php” manual=”if%20(!is_dir(%24dir))%20%0A%7B%0A%20%20%20%20mkdir(%24dir)%3B%20%20%20%20%20%20%20%20%20%0A%7D%0AOr%20should%20I%20also%20use%20file_exists%3F%20For%20example%3A%0A%0Aif%20(!file_exists(%24dir)%20%26%26%20!is_dir(%24dir))%20%0A%7B%0A%20%20%20%20mkdir(%24dir)%3B%20%20%20%20%20%20%20%20%20%0A%7D%20%0A” message=”php code” highlight=”” provider=”manual”/]

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

[pastacode lang=”javascript” manual=”%24dirname%20%3D%20%24_POST%5B%22search%22%5D%3B%0A%24filename%20%3D%20%22%2Ffolder%2F%22%20.%20%24dirname%20.%20%22%2F%22%3B%0A%0Aif%20(!file_exists(%24filename))%20%0A%7B%0A%20%20%20%20mkdir(%22folder%2F%22%20.%20%24dirname%2C%200777)%3B%0A%20%20%20%20echo%20%22The%20directory%20%24dirname%20was%20successfully%20created.%22%3B%0A%20%20%20%20exit%3B%0A%7D%20%0Aelse%20%0A%7B%0A%20%20%20%20echo%20%22The%20directory%20%24dirname%20exists.%22%3B%0A%7D%0A” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]

  • In this scenario, we want to create a folder called “images”:
[pastacode lang=”php” manual=”%3C%3Fphp%0A%0A%2F%2FThe%20name%20of%20the%20directory%20that%20we%20need%20to%20create.%0A%24directoryName%20%3D%20’images’%3B%0A%0A%2F%2FCheck%20if%20the%20directory%20already%20exists.%0Aif(!is_dir(%24directoryName))%7B%0A%20%20%20%20%2F%2FDirectory%20does%20not%20exist%2C%20so%20lets%20create%20it.%0A%20%20%20%20mkdir(%24directoryName%2C%200755)%3B%0A%7D%0A” message=”php code” highlight=”” provider=”manual”/]

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.:
[pastacode lang=”javascript” manual=”if%20(file_exists(‘file’))%20%0A%7B%0A%20%20%20%20if%20(!is_dir(‘file’))%20%0A%7B%20%0A%2F%2Fif%20file%20is%20already%20present%2C%20but%20it’s%20not%20a%20dir%0A%20%20%20%20%20%20%20%20%2F%2Fdo%20something%20with%20file%20-%20delete%2C%20rename%2C%20etc.%0A%20%20%20%20%20%20%20%20unlink(‘file’)%3B%20%2F%2Ffor%20example%0A%20%20%20%20%20%20%20%20mkdir(‘file’%2C%20NEEDED_ACCESS_LEVEL)%3B%0A%20%20%20%20%7D%0A%7D%0A%20else%0A%20%7B%20%2F%2Fno%20file%20exists%20with%20this%20name%0A%20%20%20%20mkdir(‘file’%2C%20NEEDED_ACCESS_LEVEL)%3B%0A%7D%0A” message=”javascript code” highlight=”” provider=”manual”/] [ad type=”banner”]

Sample code:

[pastacode lang=”javascript” manual=”%24year%20%3D%20date(%22Y%22)%3B%20%20%20%0A%24month%20%3D%20date(%22m%22)%3B%20%20%20%0A%24filename%20%3D%20%22..%2F%22.%24year%3B%20%20%20%0A%24filename2%20%3D%20%22..%2F%22.%24year.%22%2F%22.%24month%3B%0A%0Aif(file_exists(%24filename))%0A%7B%0A%20%20%20%20if(file_exists(%24filename2)%3D%3Dfalse)%0A%7B%0A%20%20%20%20%20%20%20%20mkdir(%24filename2%2C0777)%3B%0A%20%20%20%20%7D%0A%7D%0Aelse%0A%7B%0A%20%20%20%20mkdir(%24filename%2C0777)%3B%0A%7D%0A” message=”javascript code” highlight=”” provider=”manual”/]

  • 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:
[pastacode lang=”javascript” manual=”namespace%20com%5Cstackoverflow%3B%0A%0Aset_error_handler(function(%24errno%2C%20%24errm)%20%0A%7B%20%0A%20%20%20%20if%20(strpos(%24errm%2C%22exists%22)%20%3D%3D%3D%20false)%20throw%20new%20%5CException(%24errm)%3B%20%2F%2For%20better%3A%20create%20your%20own%20FolderCreationException%20class%0A%7D%0A)%3B%0Amkdir(%24folder)%3B%0A%2F*%20possibly%20more%20mkdir%20instructions%2C%20which%20is%20when%20this%20becomes%20useful%20*%2F%0Arestore_error_handler()%3B%0A” message=”javascript code” highlight=”” provider=”manual”/]

 

Categorized in: