PHP | mkdir( ) Function
Last Updated :
11 Jul, 2025
Improve
The mkdir() creates a new directory with the specified pathname. The path and mode are sent as parameters to the mkdir() function and it returns TRUE on success or FALSE on failure.
The mode parameter in mkdir() function is ignored on Windows platforms.
Syntax:
php
Output:
php
Output:
php
Output:
mkdir(path, mode, recursive, context)Parameters Used: The mkdir() function in PHP accepts four parameters.
- path : It is a mandatory parameter which specifies the path.
- mode : It is an optional parameter which specifies permission.
The mode parameter consists of four numbers:
- The first number is always zero.
- The second number specifies permissions for the owner.
- The third number specifies permissions for the owner's user group.
- The fourth number specifies permissions for everybody else.
- 1 = execute permissions
- 2 = write permissions
- 4 = read permissions
- recursive: It is an optional parameter which can be used to set recursive mode.
- context : It is an optional parameter which specifies the behavior of the stream.
- Mode parameter in the mkdir() function must be specified in octal representation making it lead with a zero.
- An E_WARNING level error is generated if the directory already exists.
- An E_WARNING level error is generated if the relevant permissions prevent creating the directory.
Input : mkdir("/documents/gfg/articles/");
Output : 1
Input : mkdir("/documents/gfg/articles/", 0770)
Output : 1
Input : $nest = './node1/node2/node3/';
if (!mkdir($nest, 0777, true))
{
echo('Folders cannot be created recursively');
}
else
{
echo('Folders created recursively');
}
Output : Folders created recursively
Below programs illustrate the mkdir() function.
Program 1
<?php
// making a directory with default mode i.e 0777
mkdir("/documents/gfg/articles/");
?>
1Program 2
<?php
// making a directory with the provision of all
// permissions to the owner and the owner's user group
mkdir("/documents/gfg/articles/", 0770)
?>
1Program 3
<?php
$nest = './node1/node2/node3/';
// creating a nested structure directory
if (!mkdir($nest, 0777, true))
{
echo('Folders cannot be created recursively');
}
else
{
echo('Folders created recursively');
}
?>
Folders created recursivelyRelated Article: PHP | rmdir( ) Function Reference: https://www.php.net/manual/en/function.mkdir.php
Article Tags :