mkdir
Creates a folder.
Syntax
mkdir(string $directory, int $permissions = 0777, bool $recursive = false): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $directory | string | The path of the new folder. |
| $permissions | int | Optional. The access permissions; ignored on Windows. |
| $recursive | bool | Optional. `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
- is_dir — Checks whether a path is a folder.
- file_put_contents — Writes data to a file.
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.