Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

mkdir

Creates a folder.

Syntax

mkdir(string $directory, int $permissions = 0777, bool $recursive = false): bool

Parameters

ParameterTypeDescription
$directorystringThe path of the new folder.
$permissionsintOptional. The access permissions; ignored on Windows.
$recursiveboolOptional. `true` creates the whole chain of nested folders.

Return value

bool`true` if the folder was created.

Examples

Nested folders in one call:

<?php
 
$dir = sys_get_temp_dir() . '/ref-shop/images/small';
 
if (!is_dir($dir)) {
    mkdir($dir, 0777, true);
}
 
var_dump(is_dir($dir));

Output:

bool(true)

Notes

Creating a folder that already exists raises a warning, so is_dir() is checked before the call.

See also

Related recipes

In the tutorial

  • Folders and paths — Working with folders in PHP — creating them, listing files, searching by a mask, parts of a path and file information.

Thanks for reading!

Donate to the site — we work hard for you.