forked from aidanlister/code
/
function.mkdirr.php
36 lines (31 loc) · 1.04 KB
/
function.mkdirr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
/**
* Create a directory structure recursively
*
* @author Aidan Lister <aidan@php.net>
* @version 1.0.2
* @link http://aidanlister.com/2004/04/recursively-creating-directory-structures/
* @param string $pathname The directory structure to create
* @return bool Returns TRUE on success, FALSE on failure
*/
function mkdirr($pathname, $mode = 0777)
{
// 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;
}