The Wayback Machine - http://web.archive.org/web/20231211184737/https://www.geeksforgeeks.org/php-mkdir-function/
Open In App
Related Articles

PHP | mkdir( ) Function

Improve Article
Improve
Save Article
Save
Like Article
Like

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


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 Jun, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials