Aidan's PHP Repository

A repository for PHP functions and classes ...

function.mkdirr.php

Create a directory structure recursively

  • Author: Aidan Lister <aidan@php.net>
  • Version: 1.0.1
  • Link: http://aidanlister.com/repos/v/function.mkdirr.php
  • Return: bool Returns TRUE on success, FALSE on failure
  • Views: 18655
  • Downloads: 2071

Source

Download this script <?php
/**
 * Create a directory structure recursively
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.1
 * @link        http://aidanlister.com/repos/v/function.mkdirr.php
 * @param       string   $pathname    The directory structure to create
 * @return      bool     Returns TRUE on success, FALSE on failure
 */
 
function mkdirr($pathname, $mode = null)
{
    // Check if directory already exists
    if (is_dir($pathname) || empty($pathname)) {
        return true;
    }
 
    // Ensure a file does not already exist with the same name
    $pathname = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $pathname);
    if (is_file($pathname)) {
        trigger_error('mkdirr() File exists', E_USER_WARNING);
        return false;
    }
 
    // Crawl up the directory tree
    $next_pathname = substr($pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR));
    if (mkdirr($next_pathname, $mode)) {
        if (!file_exists($pathname)) {
            return mkdir($pathname, $mode);
        }
    }
 
    return false;
}
 
?>

Comments

March 31st, 2006
if using win DIRECTORY_SEPARATOR will be \ but people coding will commonly still use \ if you pass a 'foo/bar/baz to your function, it will fail on win because it is trying to locate DIRECTORY_SEPARATOR with strrpos, which fails. consider: $pathname = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $pathname); [Editor's Note: Good pick up. Fixed, thanks!]
April 13th, 2005
Nice 1 Mate Came in real handy, couldnt find the thing anywhere.
March 31st, 2005
Great function !!! But I found another one which is simpler :
March 19th, 2005
I use php under windows (CLI) however, I use forward slashes exclusively as this will still remain compatible with *nix systems. The php manual states that: "On Windows, both slash (/) and backslash (\) are used as path separator character. In other environments, it is the forward slash (/)." A small change to this function will make this function work on both windows and *nix. put this line at the top of the function: $pathname = str_replace( "\\", "/", $pathname ); then where $next_pathname is set, use a "/" in place of the constant or variable I made this change on my system and it works fine. Thanks for a nice, very useful function. -Tim Gallagher [Editor's Note: By using the DIRECTORY_SEPARATOR separator constant, we avoid the need to hardcode any specific slash.]
March 17th, 2005
Thank you for your help
February 4th, 2005
Another slight variation. True single function return, and function name is now mkdirTree. * @version 1.0.0 * @param string $pathname The directory structure to create * @return bool Returns TRUE on success, FALSE on failure */ function mkdirTree($pathname, $mode = null) { $dirSeperator = "/"; $return = false; // Check if directory already exists if (is_dir($pathname) || empty($pathname)) { $return = true; } else { // Ensure a file does not already exist with the same name if (is_file($pathname)) { trigger_error('mkdirTree() Directory $pathname already exists', E_USER_WARNING); $return = false; } else { // Crawl up the directory tree $nextPathname = substr($pathname, 0, strrpos($pathname, $dirSeperator)); if ($this->mkdirTree($nextPathname, $mode)) { if (!file_exists($pathname) and strpos($pathname, ".htm") == 0) { echo "Making directory $pathname...
"; $return = mkdir($pathname, $mode); } } } } return $return; } ?>
January 7th, 2005
As per the 'return once' convention, I have modified the function to return once:
December 25th, 2004
good stuff
December 22nd, 2004
Nice function! I found you link on php.net! Very helpfull, it saved my day! ;)
December 16th, 2004
how to call this funtion
November 24th, 2004
Hello, it seems that DIRECTORY_SEPARATOR doesn't work on some systems. This is the case for my ISP (PHP 4.0.5, Apache 1.3.19, I don't know the exact OS type). [Editor's Note: That constant is not defined until PHP 4.3.0. You can either set it manually in the code, or use the PEAR package PHP_Compat (http://pear.php.net/package/PHP_Compat) ...]
November 10th, 2004
To use with windows change the slash to \. Thank you [Editor's Note: I updated the code to make this work on both systems]
November 5th, 2004
This is one nice little function thx
November 5th, 2004
Nice function, thx a lot :)
November 5th, 2004
thanks for the help
November 5th, 2004
Nice script
November 5th, 2004
Aidan, Thank you so very much for this wonderful function. I've spent an inordinate amount of time looking for a quick solution to mkdir's recursive deficiency (at least prior to 5.0), and you've supplied the answer. Thank you!