PHP Directory mkdir() Function (With Examples)

In this tutorial, we will learn about the PHP mkdir() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP mkdir() function

The full form of mkdir is "Make Directory", the function mkdir() is used to create a directory.

Syntax

The syntax of the mkdir() function:

mkdir(dir_path, access_mode, recursive, context);

Parameters

The parameters of the mkdir() function:

  • dir_path – It defines the path to the directory, where we want to create a directory.
  • access_mode – It is an optional parameter; its default value is 0777 that stands for the widest possible access. There are 4 values to be set for the access mode,
    • The first value should be 0
    • The second value sets the permission for the owner
    • The third value sets the permission for the owner's user group
    • The fourth value sets the permission for everybody else
      The values are 1 for execute permission, 2 for write permission, 4 for reading permission, we can add values to set the specific permissions, for example, 1+2+4 = 7 = permission for executing, write and read.
    Note: The access_mode parameter is ignored on Windows system.
  • recursive – It is also an optional parameter; It defines the recursive mode.
  • context -  It is also an optional parameter; It sets the context (a set of options that can modify the behavior of a stream) of the file handling.

Return Value

It returns a Boolean value, "TRUE" – if the directory creates successfully or "FALSE" – if the directory does not create.

PHP mkdir() Function Example

PHP code to create directory

<?php
//creating the directory
$ret_value = mkdir("/home/folder1");

if ($ret_value == true) {
    echo "directory created successfully...";
} else {
    echo "directory is not created successfully...";
}
?>

Output

The output of the above example is:

directory created successfully...

Reference: PHP mkdir() function

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.