Share
mkdir()
is a built-in function in PHP.
The mkdir()
function creates a new directory within the specified path with a specified mode that sets permissions for the directory.
The following is the function prototype:
mkdir(path, mode, recursive, context)
The mkdir()
function takes the following input parameters:
path
: The path of the file that is created.
mode
: Optional. A sequence of four numbers that specifies the permissions for the file. A breakdown of the four numbers is given below:
0
.The table below shows possible values for each number:
Value | Description |
1 | Execute permissions |
2 | Write permissions |
4 | Read permissions |
Note: To set multiple permissions, we can add the above numbers. For example,
7
(1
+2
+4
) specifies all permissions. The defaultmode
is set to0777
.
recursive
: Optional. Used to set recursive mode.
context
: Optional. Specifies the
The function returns TRUE
on successful execution. Otherwise, the function returns FALSE
.
<?php// Creating a simple directory. Default mode is 0777echo mkdir("./Edpresso");echo "\n";// Creating directory in recursive mode with different permissionsecho mkdir ("./Edpresso/myDirectory", 0700, true);?>
In the above example, we start by creating a new directory, Edpresso
. An output of 1
shows that the function was executed successfully.
Then, we create a new directory within our first directory with the path ./Edpresso/myDirectory
. This directory is created in recursive mode.