So in creating a script to reorganize folder structure, I created this function. It works and its very simple, just wondering what you guys thought of it, and how I might be made better. What it does is takes a path and checks if it exists, if it does it ends, if not it checks the parent directory for existence. It then creates the missing directory structure.

function checkDir($path) {
	if( is_dir($path) ) return;
	$parent = dirname($path);
	if( !is_dir($parent) ) checkDir($parent);
	mkdir($path);
}

The one thing I haven't been able to figure out is how to error out if its impossible to make the directory. For example, I could pass Z:/somefolder to it and it wouldn't be able to create a Z drive heh, but if I pass C:/somefolder it will create somefolder without fail.

Thoughts? TIA

    Maybe something like this (untested)?

    <?php
    function checkDir($path)
    {
       if(empty($path)) {
          user_error("Empty path, recursed to nothing?");
          return false;
       }
       if (is_dir($path)) {
          return true;
       }
       $parent = dirname($path);
       if (!is_dir($parent)) {
          return checkDir($parent);
       }
       if (is_writable($parrent)) {
          return mkdir($path);
       }
       user_error("'$parent' is not writable");
       return false;
    }
    
         $parent = dirname($path);
         if (!is_dir($parent)) {
            return checkDir($parent);
         }

      That part confuses me, because it feels like if it has to recurse it won't make all the directories. Forexample I pass the path 'C:/notafolder/notasubfolder/another' won't it just make C:/notafolder instead of the subfolders as well?

        Couldn't sleep and came back to this because it occurred to me that you should be able to just create the directory. The key is to set the "recursive" flag in the call to mkdir():

        <?php
        function checkDir($path)
        {
           if (!file_exists($path)) {
              $dir = dirname($path);
              if (file_exists($dir)) {
                 if(!is_dir($dir)) {
                    user_error("'$dir' exists and is not a directory");
                    return false;
                 }
              }
              elseif(!mkdir($dir, 0, true)) {
                 user_error("Could not create directory '$dir'");
                 return false;
              }
              if(!touch($path)) {
                 user_error("Could not create file '$path'");
                 return false;
              }
           }
           return true;
        }
        
        $file = "./new_dir/another_dir/test.txt";
        $result = checkDir($file);
        if($result) {
           $dir = glob($file);
           echo "<pre>".print_r($dir,1)."</pre>";
           unlink($file);
        }
        else {
           echo "Oops!";
        }
        

          So basically what you're saying... is I didn't need to make this function at all... just use more options with mkdir... I guess I should've RTFM when I got the error, instead of making a function. Oh well, thanks for the help as always Nog, I really like what you did with the second one!

            I didn't know about that mkdir() option until last night, either. 🙂 It just seemed to me that there had to be something better for what should be a fairly routine need, and sure enough: PHP came through for me once again.

              Write a Reply...