Thursday, September 4, 2025
HomeLanguagesPHP | mkdir( ) Function

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




<?php 
// making a directory with default mode i.e 0777
mkdir("/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 group
mkdir("/documents/gfg/articles/", 0770)
?>


Output:

1

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


Output:

Folders created recursively

Related Article: PHP | rmdir( ) Function

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS