PHP mkdir() Function

What is PHP mkdir() Function?

If you want to create a new directory, use mkdir() function. The function creates the directory in the path that you specify in the parameter.

Syntax:

mkdir(directory, permission, recursive, context)

Parameters:

The Function has 1 required parameter and 1 optional parameter-

directory (Required): It is the path to directory that you want to create. It could be an absolute path or relative path to the current script. Check example 1.

permission (Optional): Set this permission to the directory that you create. It’s default value is “0777” which ensures full access to this directory to everyone. In the next section, you’ll learn a bit more about permission. Check example 2.

recursive (Optional): Usually, the mkdir() function attempt to create the last directory mentioned in your provided directory path. If the previous directories of the directory path don’t exist, the function can’t execute properly. Well, this happens when this parameter is empty or FALSE.  Check example 3.

On the other hand, if you mention TRUE in this parameter, the functions create any non-exist parent directories in the directory path. Check example 4.

Return Values:

The function returns-

  • TRUE – on success and
  • FALSE – on failure. Check example 5.

What is File/Folder Permission & How it Works?

In Unix based operating system, the file control mechanism determines –

  • Class: who can access a file/folder and
  • Permission: what they can do with it.

Class: There are three user classes of a file/folder –

  • Owner: Owner of the file/folder,
  • Group: Those who share the same permission, and
  • Others: All the users except owner and group.

Permission: It determines what a user class can do with a file/folder. Unix based operating systems implement three levels of permission to each user class-

  • Read: User class with read permission can only read the file or view the content of the folder.
  • Write: User class with write permission can edit a file. In case a folder, the user class can add a file, delete a file and can also rename a file.
  • Execute: User class with execute permission can run a file. In case of folder, the user class can search a folder with file name. Note to list the file inside a folder, the user class need read permission.

Permission Number: Unix permission can be represented in octal notation. Basically, it has three digits like 775 or 777. In it-

  • First one is for owner,
  • Second is for group, and
  • Third one is for others.

In this octal numeral system (which uses 0 to 7 digits)-

  • 4 represents “Read” permission,
  • 2 represents “Write” permission,
  • 1 represents “Execute” permission, and
  • 0 represents “No” permission.

You can sum above numbers to assign permissions to a user class. For example, to provide only “Read” and “Execute” permissions to a user group, add 4+1 = 5. Thus assign –

  • 0 (0+0+0) for “No” permission.
  • 1 (0+0+1) for “Execute” permission.
  • 2 (0+2+0) for “Write” permission.
  • 3 (0+2+1) for “Write” & “Execute” permissions.
  • 4 (4+0+0) for “Read” permission.
  • 5 (4+0+1) for “Read” & “Execute” permissions
  • 6 (4+2+0) for “Read” & “Write” permissions
  • 7 (4+2+1) for “Read”, “Write”, and “Execute” permissions.

Examples:

Example 1:

<?php
mkdir('docs/official/');
?>

Explanation:

The function creates the directory “official” in the “docs” directory. Note, there is already a directory named “docs” in the current directory.

Example 2:

<?php
mkdir("docs/official/", 0775);
?>

Explanation:

The function creates the directory “official” in the “docs” directory with the permission 775. So, the owner and group can read, write, and execute permission. And, others have only read and write permission. Note, there is already a directory named “docs” in the current directory.

Example 3:

<?php
mkdir("docs/official/pdf/", 0777, FALSE);
?>

Output:

Warning: mkdir(): No such file or directory in D:\xampp\htdocs\php\mkdir.php on line 2

Explanation:

The function can’t create the directory “pdf” as there is no directory named “official”. Note, the third parameter (recursive) FALSE can’t create the “official” before the “pdf” directory here.

Example 4:

<?php
mkdir("docs/official/pdf/", 0777, TRUE);
?>

Explanation:

The function can create the directory “pdf” inside the “official” directory. Note, the third parameter (recursive) TRUE creates the “official” before the “pdf” directory here.

Example 5:

<?php
var_dump(mkdir('docs/official2/'));
?>

Output:

Warning: mkdir(): No such file or directory in D:\xampp\htdocs\php\mkdir.php on line 2
bool(false)

Explanation:

As there is no folder named “docs” in the current directory, the function can’t create the “officlial2” directory in it. So it returns FALSE and a warning of “No such file or directory”.

Example 6:

<?php
mkdir(__DIR__."/docs/official/");
?>

Explanation:

The function creates the folder “official” in the existing “docs” directory which exists in the current directory. Note, __DIR__ indicates the current directory.

Best Practices on mkdir() Function:

  • The function may not create a directory if there is any issue. So, it is safe to test whether the directory has been created before proceeding to the next step. Check the following example- ṁ
    <?php
    if(@mkdir("docs/official2/")){
        //Proceed to the next steps.
    }else{
        echo "Error! Directory not created.";
    }
    ?>
    

    Output:

    Error! Directory not created.

    Explanation:

    As there is no folder named “docs” in the current directory, the function can’t create the “officlial2” directory in it. So the program flow goes to the else condition. Note, we use @ before the function name to suppress any the message.

Notes on mkdir() Function:

  • In the directory parameter, you can use PHP __DIR__ constant. It indicates the directory of the file. So, you can use this along with the relative path where you want to create the directory. Check example 6.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP () Function

mkdir() is a very useful function to create directories on the fly. It is one of the built-in PHP file system functions.

Reference:

https://www.php.net/manual/en/function.mkdir.php