note 38398 deleted from function.mkdir by aidan

From: Date: Sun, 03 Oct 2004 13:38:24 +0000
Subject: note 38398 deleted from function.mkdir by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-77830@lists.php.net to get a copy of this message
Note Submitter: info at phplabs dot com ---- The following code should simulate the unix command mkdir -p to a fair degree. If no part of the path is writable, the recursion will complete gracefully without the directory being created. When running through Apache, it will require some directory along the way to be chmod 777 or chowned to "nobody." Example: suppose you have the directory /home/you/public_html and you want to create the tree /home/you/public_html/foo/bar/baz/some/long/path. Your host doesn't allow exec() but you don't want to manually issue a mkdir() for each step. You can pass the target directory to the following function. Returns 0 on failure, 1 on success. Note that I chose to report failure if the specified path is already there, you may want to see that as a success. <? function mkdir_p($target){ if(file_exists($target) || is_dir($target)) return 0; if(substr($target, 0, 1) != '/') $target = "/$target"; if(eregi('\.\.', $target) || eregi("[^a-zA-Z0-9\._\-\/]", $target)) return 0; if(mkdir($target)){ return 1; } if(mkdir_p(substr($target, 0, (strrpos($target, '/')))) == 1){ if(mkdir_p($target) == 1) return 1; else return 0; } else{ return 0; } } ?> Enjoy Shaun

« previous php.notes (#77830) next »