PHP create nested directories

Viewed 27413

I need help with a function to create a 2 level directory for the following situations:

  1. The desired sub-directory exists in the parent directory, do nothing.
  2. Parent directory exists, sub-directory does not exist. Create only the sub-directory.
  3. Neither parent directory, nor the sub-directory exists, First create parent directory, then sub-directory.
  4. If Any of the directory was not created successfully, return FALSE.

Thanks for the help.

10 Answers

You can use is_dir and mkdir

if (!is_dir($path)) 
{
    @mkdir($path, 0777, true);
}
Related