PHP | mkdir( ) Function

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.



  1. path : It is a mandatory parameter which specifies the path.
  2. 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 permissions
    • 2 = write permissions
    • 4 = read permissions

    Multiple permissions can be set by adding up the following numbers.

  3. recursive: It is an optional parameter which can be used to set recursive mode.
  4. 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:

  1. Mode parameter in the mkdir() function must be specified in octal representation making it lead with a zero.
  2. An E_WARNING level error is generated if the directory already exists.
  3. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// making a directory with default mode i.e 0777
mkdir("/documents/gfg/articles/");
?>

chevron_right


Output:

1

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// making a directory with the provision of all
// permissions to the owner and the owner's user group
mkdir("/documents/gfg/articles/", 0770)
?>

chevron_right


Output:

1

Program 3

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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');
  }
?>

chevron_right


Output:

Folders created recursively

Related Article: PHP | rmdir( ) Function

Reference:
http://php.net/manual/en/function.mkdir.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.