public function makeDirPath($path)
{
$directories = explode("/", $path);
array_shift($directories);
$filename = array_pop($directories);
$dirpath = "/";
$full = "/" . implode('/', $directories);
// find the first existing folder backwards
for ($i = count($directories); $i > 0; $i--) {
if ($this->files->exists($full)) {
break;
}
$lastpart = substr(strrchr($full, '/'), 1);
$full = substr($full, 0, -strlen($lastpart) - 1);
}
// Build path and create directories if needed
for ($k = 0; $k < count($directories); $k++) {
$dirpath .= $directories[$k] . "/";
if ($k < $i) {
continue;
}
if (!$this->files->exists($dirpath)) {
try {
$this->files->makeDirectory($dirpath);
} catch (Exception $e) {
$this->errors[] = $e->getMessage() . " for {$dirpath}";
}
}
}
}