PHP | mkdir( ) Function

01 Jun, 2018
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:
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.The set of possible values are :1 = execute permissions2 = write permissions4 = read permissionsMultiple permissions can be set by adding up the following numbers.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.
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.The set of possible values are :1 = execute permissions2 = write permissions4 = read permissionsMultiple permissions can be set by adding up the following 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.
The set of possible values are :
1 = execute permissions
2 = write permissions
4 = read permissions
Multiple permissions can be set by adding up the following numbers.
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.
Return Value:It returns TRUE on success or FALSE on failure.
Errors And Exception:
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.
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.
Examples:
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 0777mkdir("/documents/gfg/articles/");?>
Output:
1
Program 2
<?php // making a directory with the provision of all// permissions to the owner and the owner's user groupmkdir("/documents/gfg/articles/", 0770)?>
Output:
1
Program 3
<?php $nest = './node1/node2/node3/';// creating a nested structure directoryif (!mkdir($nest, 0777, true))   {     echo('Folders cannot be created recursively');  }else  {     echo('Folders created recursively');  }?>
Output:
Folders created recursively
Related Article: PHP | rmdir( ) Function
Reference:http://php.net/manual/en/function.mkdir.php
PHP-file-handling
PHP-function
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.