Skip to content

Core\IO::makeDirectory

Core\IO::makeDirectory(string $path): voidCore\IO::makeDirectory(
    string $path,
): void

Creates a folder, and every missing folder above it.

Core\IO::makeDirectory makes sure a folder exists at the path. If folders on the way to it are missing, makeDirectory creates them too. For example, makeDirectory("logs/2026/09") creates logs, logs/2026 and logs/2026/09 when none of them exist.

When the folder is already there, makeDirectory does nothing and does not throw an error. So you can call it before every write, without checking first.

makeDirectory throws an IOError when a part of the path is a file. It also throws an IOError when the operating system does not allow the program to create a folder there. An empty path throws a RuntimeError.

A program you start from the command line may write any path. On a server, a program may write only in the folders that Core\IO::createTemporaryDirectory made for it. Any other path needs the fs.write capability. Without it, makeDirectory throws a RuntimeError and creates nothing.

Parameters

  • $pathstringThe directory that must exist. Every missing directory above it is created too. All of them are inside the path the capability is checked for.

Return value

Returns void. Nothing. A directory that already exists is a success, not an error. The method only promises that the directory exists afterwards, so a caller needs no exists check first. removeDirectory works differently. It does not delete directories recursively.

Errors

  • RuntimeErrorThe configuration does not grant fs.write for this path. This is checked before anything is created, so the failed call leaves no directory behind.
  • IOErrorThe capability allowed the call, but the operating system did not. A part of the path exists and is a file, or the process may not create a directory there.

Changelog

VersionChange
0.0.6Core\IO writes the word "directory" in full: createTemporaryDirectory, makeDirectory, removeDirectory and isDirectory. Core\IO::metadata returns the Core\IO\Metadata of a file. The old names were temporaryDir, makeDir, removeDir, isDir and stat.

Examples

Create nested folders

<?nvs
// Shows that `Core\IO::makeDirectory` creates a folder and every missing folder above it.
string $root = Core\Path::join(Core\IO::createTemporaryDirectory(), 'project');
string $deep = Core\Path::join($root, 'src', 'views', 'admin');
// One call creates "project", "src", "views" and "admin".
Core\IO::makeDirectory($deep);
// This prints "project/src: true" and then "project/src/views/admin: true".
echo 'project/src: ', Core\IO::isDirectory(Core\Path::join($root, 'src')) ? 'true' : 'false', "\n";
echo 'project/src/views/admin: ', Core\IO::isDirectory($deep) ? 'true' : 'false', "\n";
Output
project/src: true
project/src/views/admin: true

A folder already there

<?nvs
// Shows that `Core\IO::makeDirectory` accepts a folder that already exists, and throws when a file is in the way.
string $dir = Core\IO::createTemporaryDirectory();
string $cache = Core\Path::join($dir, 'cache');
// The second call finds the folder already there. It does nothing, so this prints "ready".
Core\IO::makeDirectory($cache);
Core\IO::makeDirectory($cache);
echo 'ready', "\n";
// "notes.txt" is a file, so there cannot be a folder inside it.
// `makeDirectory` throws an `IOError`, and this prints "a file is in the way".
string $notes = Core\Path::join($dir, 'notes.txt');
Core\IO::write($notes, "hello\n");
try {
Core\IO::makeDirectory(Core\Path::join($notes, 'old'));
} catch (IOError $e) {
echo 'a file is in the way', "\n";
}
Output
ready
a file is in the way

Store uploads by month

<?nvs
// Shows how a program stores each uploaded file in a folder for its year and month.
class Uploads
{
// Creates the folder for the month if it is missing, and then writes the file into it.
// It returns the path of the file, relative to the uploads folder.
public static function store(string $root, string $year, string $month, string $name, string $content): string
{
string $folder = Core\Path::join($root, $year, $month);
Core\IO::makeDirectory($folder);
Core\IO::write(Core\Path::join($folder, $name), $content);
return $year . '/' . $month . '/' . $name;
}
}
string $root = Core\Path::join(Core\IO::createTemporaryDirectory(), 'uploads');
// The first two files go into the same folder. The second call finds the folder already there.
// This prints "2026/09/invoice.pdf", "2026/09/photo.jpg" and "2026/10/contract.pdf".
echo Uploads::store($root, '2026', '09', 'invoice.pdf', '...'), "\n";
echo Uploads::store($root, '2026', '09', 'photo.jpg', '...'), "\n";
echo Uploads::store($root, '2026', '10', 'contract.pdf', '...'), "\n";
Output
2026/09/invoice.pdf
2026/09/photo.jpg
2026/10/contract.pdf