PHP Tutorial - PHP mkdir() Function


The mkdir() function creates a new directory .


PHP mkdir() Function has the following syntax.



ParameterIs RequiredDescription
pathRequired.Name of the directory to create
modeOptional.Permissions. By default, the mode is 0777 (widest possible access).
recursiveOptional.If the recursive mode is set
contextOptional.Context of the file handle. Context is a set of options that can modify the behavior of a stream

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

Possible values (to set multiple permissions, add up the following numbers):

  • 1 = execute permissions
  • 2 = write permissions
  • 4 = read permissions


Returns TRUE on success or FALSE on failure.


The function returns true if the directory was created successfully or false otherwise. For example:

     mkdir("/path/to/my/directory", 0777);                                                           
     mkdir("/path/to/my/directory", 0777, true);